【发布时间】:2016-03-31 04:09:55
【问题描述】:
我正在尝试填写 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 = {
^ ~
【问题讨论】:
-
@TonyD 我忘了补充,我用的是
using namespace std; -
那么您可能还遗漏了其他重要的内容。请包含逐字的、未删节的代码作为最小的完整示例。
-
你
#include <string>了吗? -
这里没有错误。看到这个->ideone.com/RpqIhx
-
@BrianCain ,我相信我已经包含了与问题相关的所有内容。此外,这些定义在我的 file.h 文件中。它们应该在 file.cc 文件中吗?有关系吗?
标签: c++ dictionary