簡體   English   中英

將枚舉與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

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM