error: switch quantity not an integer

In switch, the expression must be of “an integral type or of a class type for which there is an unambiguous conversion to integral type” (quoting VS2008 docs).

A string class doesn’t have “unambiguous conversion to integral type”, like a char does.

As a work-around:

  1. Create a map<string, int> and switch on the value of the map: switch(command_map[command]) `
  2. Do a set of if/else instead of switch. Much more annoying and hard to read, so I’d recommend the map route.

As an aside, an even better solution for really complicated logic like that is to improve the mapping solution to get rid of switch completely and instead go with a function lookup: std::map<std::string, functionPointerType>. It may not be needed for your specific case, but is MUCH faster for complicated very long look-up logic.

Leave a Comment

tech