C++错误:"Expression must have integral or enum type"

C++ Error: "Expression must have integral or enum type"

本文关键字:or integral enum type have must 错误 Expression C++      更新时间:2023-10-16

我得到错误"表达式必须具有整型或枚举类型"对我的(不完整)函数的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":
        }
    }
}

错误已清除。您只能使用整型类型(integer, enum, char可转换为integral值),或在switch语句中计算为整型的任何表达式

params[0]的类型为std::string。不能使用std::string类型(它不是整型)作为switch参数。如果您确信字符串不是空的,则使用switch (param[0][0])case 'd'。但在这种情况下,您将只能切换单字符字符串。如果需要切换较长的字符串,则需要使用if-else if-else if-...序列。