我正在尝试填充std::map
,但是出现2个编译器错误,并且我不知道是什么原因。
std::map<std::string, std::string> dirFull;
dirFull["no"] = "north";
dirFull["so"] = "south";
dirFull["ea"] = "east";
dirFull["we"] = "west";
dirFull["nw"] = "north-west";
dirFull["ne"] = "north-east";
dirFull["sw"] = "south-west";
dirFull["se"] = "south-east";
这些是错误:
error: C++ requires a type specifier for all declarations
dirFull["no"] = "north";
^
error: size of array has non-integer type 'const char[3]'
dirFull["no"] = "north";
^~~~
std::map<std::string, std::string> dirFull = {
{"no", "north"}, {"so", "south"},
{"ea", "east"}, {"we", "west"},
{"ne", "north-east"}, {"nw", "north-west"},
{"se", "south-east"}, {"sw","south-west"} };
这导致了完全不同的错误类型:
error: non-aggregate type 'std::map<std::string, std::string>' (aka '...') cannot be initialized with an initializer list
std::map<std::string, std::string> dirFull = {
^ ~
之所以出现此错误,是因为您试图在文件范围内执行语句。在函数中定义这些分配,您将不再遇到这些错误。
如果要map
在静态初始化期间填充它,可以使用boost::assign
或 初始化语法来完成。 constexpr
//requires c++11:
const map <string,string> dirFull = {
{"no", "north"},
{"so", "south"},
{"ea", "east"},
{"we", "west"},
{"nw", "north-west"},
{"ne", "north-east"},
{"sw", "south-west"},
{"se", "south-east"},
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句