[英]mixing enum with std::map<>
我一直在尝试编译以下代码,但没有成功:
enum class gType {GAME1, GAME2, GAME3};
typedef std::map<std::string, gType> gamesTypesMap;
gamesTypesMap gameTypes;
gameTypes["game_one"] = gType::GAME1;
我收到3个错误:
error: C++ requires a type specifier for all declarations gameTypes["game_one"] = gType::GAME1;
error: size of array has non-integer type 'const char [8]' gameTypes["game_one"] = gType::GAME1;
error: 'gType' is not a class, namespace, or scoped enumeration
gameTypes [“ game_one”] = gType :: GAME1;
任何帮助将不胜感激
确保包括map和string标头,并使用支持C ++ 11的编译器。 以下代码使用clang ++在我的机器上编译
#include <string>
#include <map>
int main()
{
enum class gType {GAME1, GAME2, GAME3};
typedef std::map<std::string, gType> gamesTypesMap;
gamesTypesMap gameTypes;
gameTypes["game_one"] = gType::GAME1;
return 0;
}
您缺少包含映射和字符串。 有了这些include并启用了C ++ 11支持,您的代码就可以用minGW编译器很好地编译了。
#include <map>
#include <string>
enum class gType {GAME1, GAME2, GAME3};
typedef std::map<std::string, gType> gamesTypesMap;
int main()
{
gamesTypesMap gameTypes;
gameTypes["game_one"] = gType::GAME1;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.