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

Bra*_*ans 9 c++ enums parsing map

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

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

#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;
Run Code Online (Sandbox Code Playgroud)

当我通过编译器,我得到以下内容:

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;
            ^
Run Code Online (Sandbox Code Playgroud)

这是我第一次尝试使用地图和枚举,而且我不确定我做错了什么.我对自动变速器的使用也不是非常熟悉,所以这对我来说是一些货物狂热编程.我希望我理解它的实现,一旦我解决了我遇到的类型定义问题,错误就会清除.

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

map < string, Noun > knownNouns;
knownNouns["name"]      = Verb::name;
knownNouns["base"]      = Verb::base;
knownNouns["attack"]    = Verb::attack;
Run Code Online (Sandbox Code Playgroud)

被纠正为

map < string, Noun > knownNouns;
knownNouns["name"]      = Noun::name;
knownNouns["base"]      = Noun::base;
knownNouns["attack"]    = Noun::attack;
Run Code Online (Sandbox Code Playgroud)

Che*_*Alf 15

您不能将非声明构造直接放在命名空间范围中.

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

分配等非声明语句必须在函数体内.


固定代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

knownNouns初始化的.这不是一项任务,即使=看起来非常像作业.

  • @BradleyEvans:在编译器建议时添加选项`-std = c ++ 11`(告诉它这是C++ 11代码).你可能还想添加`-Wall`,`-Wextra`和`-pedantic-errors`.IIRC.;-)请注意,在这个答案的固定代码中没有名称`Verb`.所以这不是你正在编译的代码. (2认同)