Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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.

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":

        }
    }
}
like image 603
user3629533 Avatar asked May 22 '14 17:05

user3629533


2 Answers

The error is clear. You can only use integral types (integer, enum, char etc. which are convertible to integral value), or any expression that evaluates to an integral type in switch statement.

like image 137
Rakib Avatar answered Oct 02 '22 05:10

Rakib


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-....

like image 28
Paul Avatar answered Oct 02 '22 06:10

Paul