【发布时间】:2018-02-20 14:02:20
【问题描述】:
因此,由于某种原因,这在类构造器中有效,但在类之外不起作用,我想知道为什么以及如何让我的地图在类之外工作。
#include <iostream>
#include <string>
#include <map>
typedef std::map <std::string, int> idMap;
idMap type_id;
type_id["Moon"] = 1;
type_id["Star"] = 2;
type_id["Sun"] = 3;
int main()
{
std::cout << type_id["Moon"] << std::endl;
}
我得到的编译器错误如下
11:1: error: 'type_id' does not name a type 12:1: error: 'type_id' does not name a type 13:1: error: 'type_id' does not name a type
我正在寻找一个这样的例子,如果你能告诉我为什么这不起作用。
【问题讨论】:
-
你认为
type_id["Moon"] = 1;(和其他两个)应该在什么时候执行? (语言规范不允许在函数之外这样做) -
在一个更大的程序中我正在尝试创建我希望将我的地图放入标题中,以便我可以在程序的其他部分使用它
标签: c++ dictionary compiler-errors stl stdmap