【问题标题】:Compiler error when subtracting std::strings减去 std::strings 时出现编译器错误
【发布时间】:2014-10-22 21:55:25
【问题描述】:

我是 C++ 的初学者,所以我只是在阅读文章和书籍时搞砸了一些东西。但我花了 20 分钟一遍又一遍地重新阅读这篇文章,我不知道它有什么问题。

#include <iostream>
#include <string>

using namespace std;

int main ()
{

    cout << "Hello there, this is your personal simple calculator.";
    cin.get();
    cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)"<< endl;
    string c;
    getline (cin, c);

    if (c == "Addition")
    {
        string a_1;
        string a_2;
        cout << "You chose addition. Press enter" << endl ;
        cin.get();
        cout << "Type in the first value: ";
        getline( cin, a_1);
        cout << "Type in the second value: ";
        getline (cin, a_2);
        cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl;
    }
    else
    {
        cout << "You spelled it wrong.";
        return 0;
    }
    if ( c == "Subtraction")
    {
        string s_1;
        string s_2;
        cout << "You chose subtraction. Press enter" << endl ;
        cin.get();
        cout << "Type in the first value: ";
        getline (cin, s_1);
        cout << "Type in the second value: ";
        getline (cin, s_2);
        cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl;
    }

}

我认为这是唯一的错误

42 83 C:\Users\Jason\Desktop\Lesson - Header Files\LH1.cpp [错误] no match for 'operator-' in 'first_argument - second_argument'

我不明白。加法符号有效,除减法之外的一切都有效。 所以我搞砸了别的东西

cout << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl;

但是减法部分工作正常。我不明白。请帮忙

【问题讨论】:

  • 请想一个更具描述性的标题。这个标题可能适用于数千个问题。
  • std::string 没有operator-
  • + 运算符用于 std::string 进行连接。没有operator-,我不知道你认为它应该做什么。
  • 您希望两个字符串相减会产生什么结果?将 string1 替换为 string2 为 ""?
  • 看看你的“加法”的输出。您应该注意到它没有执行算术运算,而是两个输入字符串的连接。以类似的方式,两个字符串的减法不会进行算术运算,并且您没有将减法运算符添加到字符串的派生子类中,因此编译器只是让您知道它不知道如何将两个字符串相减。跨度>

标签: c++


【解决方案1】:

string 可以处理文本。当您添加两个字符串时,它们被连接起来("2"+"2"=="22",而不是"4")。字符串没有operator-

要处理浮点数,请使用double。要处理整数,请使用int

double d1, d2;
//some output
cin  >> d1;
//some output
cin  >> d2;
cout << d1 << " - " << d2 << " = " << (d1-d2) << '\n';

【讨论】:

    【解决方案2】:

    加法部分有效,因为字符串 + 字符串导致字符串字符串。它附加两个字符串并返回一个新字符串。

    但是减去两个字符串并没有任何意义。

    我相信您真正想要做的是将字符串转换为数字,然后减去数字。

    为此,您需要使用以下内容:

    double val_1, val_2;
    cin >> val_1;
    cin >> val_2;
    
    cout << "result is " << (val_1 - val_2) << endl;
    

    我将减法放在括号内,因为我相信&lt;&lt; aka "shift" 运算符与乘法处于同一级别,这意味着如果没有它们,它将尝试评估 ("result is "

    由于我不确定运算符的优先级,我检查了http://en.cppreference.com/w/cpp/language/operator_precedence 并发现

    【讨论】:

      【解决方案3】:
      #include <iostream>
      #include <string>
      #include <limits>
      
      using namespace std;
      
      int main()
      {
      
          cout << "Hello there, this is your personal simple calculator.";
          cin.get();
          cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)" << endl;
          string c;
          getline(cin, c);
      
          if (c == "Addition")
          {
              int a_1;
              int a_2;
              cout << "You chose addition. Press enter" << endl;
              cin.get();
              cout << "Type in the first value: ";
              cin >> a_1;
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
              cout << "Type in the second value: ";
              cin >> a_2;
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
              cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl;
          }
      
          else if (c == "Subtraction")
          {
              int s_1;
              int s_2;
              cout << "You chose subtraction. Press enter" << endl;
              cin.get();
              cout << "Type in the first value: ";
              cin >> s_1;
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
              cout << "Type in the second value: ";
              cin >> s_2;
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
              cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl;
          }
          else
          {
              cout << "You spelled it wrong.";
              return 0;
          }
      }
      

      使用 if else 语句,否则您的代码将没有机会检查用户是否正在尝试减去。最好把 else 放在最后。 还将字符串更改为整数,因为字符串不能减去,因为它们不是数字。 最后一点,我使用 cin.clear() 和 cin.ignore() 来刷新 cin 缓冲区。

      【讨论】:

        猜你喜欢
        • 2014-12-08
        • 1970-01-01
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多