C++ 错误:“表达式必须具有整数或枚举类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23813144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Error: "Expression must have integral or enum type"
提问by user3629533
I'm getting the error "Expression must have integral or enum type"on the switch
statement of my (incomplete) function below. I've stared at it for a while and can't figure out what the matter is. Any insight greatly appreciated.
我在下面的(不完整)函数的语句中收到错误“表达式必须具有整数或枚举类型”switch
。我盯着它看了一会儿,不知道是怎么回事。任何见解都非常感谢。
std::string CWDriver::eval_input(std::string expr)
{
std::vector<std::string> params(split_string(expr, " "));
std::string output("");
if (params.size() == 0)
{
output = "Input cannot be empty.\n";
}
else
{
switch (params[0])
{
case "d":
}
}
}
回答by Rakib
The error is clear. You can only use integraltypes (integer
, enum
, char
etc. which are convertibleto integral
value), or any expressionthat evaluates to an integral type in switch
statement.
错误很明显。您只能使用积分类型(integer
,enum
,char
等它们转换到integral
值),或任何表达,其值在整型switch
声明。
回答by Paul
params[0]
has type of std::string
. You can't use std::string
type (which is not integral) as a switch
parameter. If you are confident strings are not empty use switch (param[0][0])
and case 'd'
. But in this case you will be able to switch over one-character strings only. If you need to switch over longer strings you need to use the sequence of if-else if-else if-...
.
params[0]
类型为std::string
. 您不能使用std::string
类型(不是整数)作为switch
参数。如果您确信字符串不是空的,请使用switch (param[0][0])
和case 'd'
。但在这种情况下,您将只能切换一个字符的字符串。如果需要切换更长的字符串,则需要使用if-else if-else if-...
.