error: switch quantity not an integer

后端 未结 7 877
你的背包
你的背包 2020-12-11 02:11

I have researched my issue all over StackOverflow and multi-google links, and I am still confused. I figured the best thing for me is ask...

Im creating a simple co

7条回答
  •  爱一瞬间的悲伤
    2020-12-11 02:29

    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 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. It may not be needed for your specific case, but is MUCH faster for complicated very long look-up logic.

提交回复
热议问题