No match for 'operator==' in a simple string code

前端 未结 4 1025
挽巷
挽巷 2021-01-28 18:20

Writing a simple code and ran into a problem I\'m not sure how to deal with. I tried looking into it with a search but nothing I found was much help and everyone\'s answers wer

4条回答
  •  独厮守ぢ
    2021-01-28 18:45

    lastTwoChars is a string, you are comparing it with an int in these statements :

             if (lastTwoChars == 41)
             { 
                  cout << "Red";
             }
             if (lastTwoChars == 25)
             { 
                  cout << "Black";
             }
             if (lastTwoChars == 30)
             { 
                  cout << "Green";
             }
    

    This is against the defined behaviour for string. You have to compare it to a string or char*.

             if (lastTwoChars == "41")
             { 
             }
                  cout << "Red";
             .
             .
             .
    

    Now "41" is a const char* in this case and it can be compared with a string or a char*.

提交回复
热议问题