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

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

游戏类型["game_one"] = gType::GAME1;

任何帮助将不胜感激

确保包含映射和字符串 headers 并使用支持 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;
}

您缺少包含映射和字符串。有了这些包含并启用了 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;
}