switch/case statement in C++ with a QString type

后端 未结 14 2083
南笙
南笙 2020-12-16 10:19

I want to use switch-case in my program but the compiler gives me this error:

switch expression of type \'QString\' is illegal

How can I us

14条回答
  •  情书的邮戳
    2020-12-16 10:50

    case "the":
        //^^^ case label must lead to a constant expression
    

    I am not aware of qt, but you can give this a try. You can avoid switch and directly use == for comparison, if QString is no different than a normal std::string.

    if( word == "the" )
    {
       // ..
    }
    else if( word == "at" )
    {
       // ..
    }
    // ....
    

提交回复
热议问题