简体   繁体   English

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

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

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 terribly familiar 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 (ie, 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;

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 knownNouns is initialized . 这里knownNouns初始化 It's not an assignment, even if the = looks very much like an assignment. 它不是一个任务,即使=看起来非常像一个任务。

You may not initialize map entries statically like: 您可能无法静态初始化地图条目,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM