【问题标题】:No match for 'operator==' in a simple string code在简单的字符串代码中不匹配 'operator=='
【发布时间】:2013-04-17 03:00:59
【问题描述】:

编写一个简单的代码并遇到了一个我不知道如何处理的问题。我尝试通过搜索对其进行调查,但没有发现任何帮助,而且每个人的答案都超出了我的想象。请有人像对小孩一样解释这个,哈哈。谢谢。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string invCode = "";
    string lastTwoChars = "";

    cout << "Use this program to determine color of furniture.";
    cout << "Enter five-character inventory code: ";
    cin >> invCode;

    if (invCode.length() == 5)
    {
        lastTwoChars = invCode.substr(3,2);
         if (lastTwoChars == 41)
         { 
              cout << "Red";
              }
         if (lastTwoChars == 25)
         { 
              cout << "Black";
              }
         if (lastTwoChars == 30)
         { 
              cout << "Green";
              }
    }
    else
         cout << "Invalid inventory code." << endl;

    system("pause"); 
    return 0;
}

【问题讨论】:

  • 错误指向哪一行?
  • 表示没有为字符串和整数的比较定义==lastTwoChars == 25无效。 lastTwoChars == "25" 就是你要找的。​​span>
  • 错误应该更具体...类似“运算符==(字符串,int)不匹配”之类的...阅读全文时更容易判断发生了什么事物。 :P

标签: c++ string operator-keyword


【解决方案1】:

lastTwoChars 是一个字符串。您必须将其与字符串进行比较,或者至少与 const char *const char[] 进行比较。

表达式lastTwoChars == 41 将lastTwoChars 与41 进行比较——int。这不是字符串的定义行为。

相反,将 41 放在引号中使其成为 const char[](特别是 const char[3]):

 if (lastTwoChars == "41")

您的代码中似乎多次执行此操作。

【讨论】:

    【解决方案2】:

    据推测,错误是抱怨您无法将字符串与数字进行比较。它们是两种不同的类型,与某些语言不同,它们之间没有神奇的转换(或比较)。

    你想和另一个字符串比较:

    if (lastTwoChars == "25")
    //                  ^  ^
    

    【讨论】:

      【解决方案3】:

      lastTwoChars 是一个 字符串,您在这些语句中将它与一个 int 进行比较:

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

      这违反了为 string 定义的行为。您必须将其与 stringchar* 进行比较。

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

      现在"41" 在这种情况下是 const char* 并且可以与 stringchar* 进行比较。

      【讨论】:

        【解决方案4】:
         #include <iostream>
         #include <string>
        
         using namespace std;
        
         int main()
         {
             string invCode = "";
             string lastTwoChars = "";
        
             cout << "Use this program to determine color of furniture.";
             cout << "Enter five-character inventory code: ";
             cin >> invCode;
        
             if (invCode.length() == 5)
             {
                 lastTwoChars = invCode.substr(3,2);
                  if (lastTwoChars == "fourty five") // you declared lastTwoChars as string therefore you have to compare letters not integers which are numbers.
                  { 
                       cout << "Red";
                  }
             if (lastTwoChars == "twenty five") //same
             { 
                  cout << "Black";
                  }
             if (lastTwoChars == "thirty") // same
             { 
                  cout << "Green";
                  }
        }
        else
             cout << "Invalid inventory code." << endl;
        
        cin.get(); // better than system("pause");
        return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-05
          • 2011-10-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多