简体   繁体   中英

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:

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;

Any help will be greatly appreciated

Be sure to include map and string headers and to use a compiler that supports C++11. The following code compiles on my machine with 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 <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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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