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":
}
}
}
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.
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-...
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With