std :: map-C ++要求所有声明都使用类型说明符

用户名

我正在尝试填充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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++要求所有声明都使用类型说明符

错误:C ++要求所有声明都使用类型说明符?

更新到Xcode9之后,我得到C ++要求所有声明都使用类型说明符

在类内创建矢量(2D数组)的矢量-错误:C ++要求所有声明都使用类型说明符

警告“C++ 要求所有声明的类型说明符”映射

如何修复错误“C++ 要求所有声明的类型说明符?”

C ++在std :: map <>中使用std :: set <>

C ++ std :: map保存任何类型的值

带有 std::map 的 C++ 向量?

是否有Java Map keySet()等效于C ++的std :: map?

为什么在std :: unordered_map :: count上没有`noexcept`说明符?

C ++ std :: map在“ *”上失败?

c ++ Std::accumulate for unordered map

C ++ | 重载运算符<< | std :: map

在C ++中将QPoint与std :: map一起使用

为什么C ++ std :: map :: operator []不使用inplace new?

在C ++ std :: map中使用模板迭代器

C#字典到C ++ std :: map

C ++检查std :: array或unordred_map / map是否包含相同的元素类型

如何使std :: fill函数与std :: map一起使用?C ++

C ++ std :: unique_ptr存储在std :: map内部,使用删除的函数会形成不良

在std :: map中使用std :: function

具有std :: map值的std :: map的问题

std :: map :: operator []比std :: map :: insert更有效?

新的std :: map :: erase()签名C ++ 17

Python dict.update()与C ++ std :: map?

在C ++ 17中更改std :: map :: insert

C ++匿名结构为std :: map值

在std :: map :: insert的C ++分段错误