简体   繁体   English

将枚举与std :: map <>混合

[英]mixing enum with std::map<>

I have been trying to compile the following code for a while with no success: 我一直在尝试编译以下代码,但没有成功:

enum class gType {GAME1, GAME2, GAME3};
typedef std::map<std::string, gType> gamesTypesMap;
gamesTypesMap gameTypes;
gameTypes["game_one"] = gType::GAME1;

I am getting 3 errors: 我收到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; gameTypes [“ game_one”] = gType :: GAME1;

Any help will be greatly appreciated 任何帮助将不胜感激

Be sure to include map and string headers and to use a compiler that supports C++11. 确保包括map和string标头,并使用支持C ++ 11的编译器。 The following code compiles on my machine with clang++ 以下代码使用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;
}

You are missing the includes map and string. 您缺少包含映射和字符串。 With these include and with C++11 support enabled, your code compiled fine for me with minGW compiler. 有了这些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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM