Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: switch quantity not an integer

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 command line calculator. Here is my code so far:

const std::string Calculator::SIN("sin");  
const std::string Calculator::COS("cos");  
const std::string Calculator::TAN("tan");  
const std::string Calculator::LOG( "log" );  
const std::string Calculator::LOG10( "log10" );

void Calculator::set_command( std::string cmd ) {

    for(unsigned i = 0; i < cmd.length(); i++)
    {
    cmd[i] = tolower(cmd[i]);
    }

    command = cmd;
}

bool Calculator::is_legal_command() const {

    switch(command)
    {
    case TAN:
    case SIN:
    case COS:
    case LOG:
    case LOG10:
        return true;
        break;
    default:
        return false;
        break;
    }

}

the error i get is:

Calculator.cpp: In member function 'bool Calculator::is_trig_command() const':  
Calculator.cpp: error: switch quantity not an integer  
Calculator.cpp: error: 'Calculator::TAN' cannot appear in a constant-expression  
Calculator.cpp: error: 'Calculator::SIN' cannot appear in a constant-expression  
Calculator.cpp: error: 'Calculator::COS' cannot appear in a constant-expression  

The mighty internet, it says strings are allowed to be used in switch statements.

Thanks everyone, I appreciate your help.

like image 671
Ken Avatar asked Dec 26 '10 23:12

Ken


3 Answers

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

like image 169
DVK Avatar answered Oct 09 '22 06:10

DVK


As others and the compiler commented, strings are not allowed with switch. I would just use if

bool Calculator::is_legal_command() const {
    if(command == TAN) return true;
    if(command == SIN) return true;
    if(command == COS) return true;
    if(command == LOG) return true;
    if(command == LOG10) return true;
    return false;
}

I don't think that's any more complicated, and it's about as fast as it could get. You could also use my switch macro, making it look like

bool Calculator::is_legal_command() const {
    sswitch(command)
    {
    scase (TAN):
    scase (SIN):
    scase (COS):
    scase (LOG):
    scase (LOG10):
        return true;

    sdefault():
        return false;
    }
}

(having break after a return is dead code, and so should be avoided).

like image 31
Johannes Schaub - litb Avatar answered Oct 09 '22 04:10

Johannes Schaub - litb


Strings cannot be used in switch statements in C++. You'll need to turn this into if/else if, like this:

if (command == "tan")
{
    // ...
}
else if (command == "cos")
{
    // ...
}
// ...
like image 29
SoapBox Avatar answered Oct 09 '22 06:10

SoapBox