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

后端 未结 14 2084
南笙
南笙 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条回答
  •  旧时难觅i
    2020-12-16 10:25

    Check out this, it helps me

    int main(int, char **)
    {
        static const uint red_hash = 30900;
        static const uint green_hash = 7244734;
        static const uint blue_hash = 431029;
      else  
        static const uint red_hash = 112785;  
        static const uint green_hash = 98619139;  
        static const uint blue_hash = 3027034;
      endif
    
        QTextStream in(stdin), out(stdout);
        out << "Enter color: " << flush;
        const QString color = in.readLine();
        out << "Hash=" << qHash(color) << endl;
    
        QString answer;
        switch (qHash(color)) {
        case red_hash:
            answer="Chose red";
            break;
        case green_hash:
            answer="Chose green";
            break;
        case blue_hash:
            answer="Chose blue";
            break;
        default:
            answer="Chose something else";
            break;
        }
        out << answer << endl;
    }
    

提交回复
热议问题