error: switch quantity not an integer

后端 未结 7 872
你的背包
你的背包 2020-12-11 02:11

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 co

7条回答
  •  一生所求
    2020-12-11 02:26

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

提交回复
热议问题