错误:c++ [map] 没有命名类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26455952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:36:25  来源:igfitidea点击:

error: c++ [map] does not name a type

c++parsingmapenums

提问by Bradley Evans

I've written the following as part of a text command parser for a text adventure game.

我已将以下内容作为文本冒险游戏的文本命令解析器的一部分编写。

I'm attempting to associate a string input by a user to an item in an enum class. The following is in my header file:

我试图将用户输入的字符串与枚举类中的项目相关联。以下是我的头文件:

#include <iostream>
#include <map>
#include <string>
using namespace std;

enum class Noun
{
    // Interrogation subjects
    name,                   // ask the subject his name
    base,                   // where is the base?
    attack,                 // when will the attack be?

    invalid
};

map < string, Noun > knownNouns;
knownNouns["name"]      = Noun::name;
knownNouns["base"]      = Noun::base;
knownNouns["attack"]    = Noun::attack;

Noun parseNoun(string &noun)
{
    auto n = knownNouns.find(noun);
    if ( n == knownNouns.end() ) {
        return Noun::invalid;
    }
    return n->second;

When I put this through the compiler, I get the following:

当我把它通过编译器时,我得到以下信息:

nouns.h:46:1: error: 'knownNouns' does not name a type
 knownNouns["name"]      = Noun::name;
 ^
nouns.h:47:1: error: 'knownNouns' does not name a type
 knownNouns["base"]      = Noun::base;
 ^
nouns.h:48:1: error: 'knownNouns' does not name a type
 knownNouns["attack"]    = Noun::attack;
 ^
nouns.h: In function 'Noun parseNoun(std::string&)':
nouns.h:52:10: error: 'n' does not name a type
     auto n = knownNouns.find(noun);
          ^
nouns.h:53:10: error: 'n' was not declared in this scope
     if ( n == knownNouns.end() ) {
          ^
nouns.h:54:16: error: 'Noun' is not a class or namespace
         return Noun::invalid;
                ^
nouns.h:56:12: error: 'n' was not declared in this scope
     return n->second;
            ^

This is my first time attempting to use maps and enums, and I'm not sure what it is I did wrong. I'm also not terriblyfamiliar with the use of auto vars, so that's a bit of cargo-cult programming on my part. I'm hoping I understood its implementation and the error will clear once I resolve the type definition problem I'm having.

这是我第一次尝试使用地图和枚举,我不确定我做错了什么。我也没有非常熟悉使用汽车的增值经销商,所以这对我而言有点货物邪教编程。我希望我理解了它的实现,一旦我解决了我遇到的类型定义问题,错误就会清除。

edit: That's embarassing. I copy pasted a mistake that I had already corrected. the code still does not compile when (i.e., the same issue occurs when)

编辑:这很尴尬。我复制粘贴了一个我已经纠正的错误。代码仍然无法编译(即,出现相同的问题时)

map < string, Noun > knownNouns;
knownNouns["name"]      = Verb::name;
knownNouns["base"]      = Verb::base;
knownNouns["attack"]    = Verb::attack;

is corrected as

更正为

map < string, Noun > knownNouns;
knownNouns["name"]      = Noun::name;
knownNouns["base"]      = Noun::base;
knownNouns["attack"]    = Noun::attack;

回答by Cheers and hth. - Alf

You can't place non-declaration constructs directly in namespace scope.

您不能直接在命名空间范围内放置非声明构造。

A C++ translation unit is a sequence of declarations.

C++ 翻译单元是一系列声明。

Non-declaration statements such as assignments must be inside function bodies.

诸如赋值之类的非声明语句必须在函数体内。



Fixed code:

固定代码:

#include <iostream>
#include <map>
#include <string>
using namespace std;

enum class Noun
{
    // Interrogation subjects
    name,                   // ask the subject his name
    base,                   // where is the base?
    attack,                 // when will the attack be?

    invalid
};

map< string, Noun > knownNouns = 
{
    { "name", Noun::name },
    { "base", Noun::base },
    { "attack", Noun::attack }
};

auto parseNoun( string const& noun )
    -> Noun
{
    // auto n = knownNouns.find(noun);
    // if ( n == knownNouns.end() ) {
        // return Noun::invalid;
    // }
    // return n->second;
    return Noun::invalid;
}

Here knownNounsis initialized. It's not an assignment, even if the =looks very much like an assignment.

这里knownNouns初始化的。这不是作业,即使=看起来很像作业。

回答by Oncaphillis

You may not initialize map entries statically like:

您不能像这样静态地初始化地图条目:

 knownNouns["name"]      = Verb::name;
 knownNouns["base"]      = Verb::base;
 knownNouns["attack"]    = Verb::attack;