【问题标题】:C++ Palindrome algorithm - No match for 'operator<'C++ 回文算法 - 'operator<' 不匹配
【发布时间】:2018-02-24 09:42:58
【问题描述】:

我下面的C++回文算法实现导致如下错误:

'operator' 和 '')

错误发生在结尾的else 行。

#include <iostream>
using namespace std;

int main()
{
    cout << "Write a number to check if its palindrome" << endl;
    int a;
    cin >> a;
    int z = a;
    int b;
    int c;
    while (a > 0) {
        b = a % 10;
        c = (c * 10) + b;
        a = a / 10;
    }
    if (z == c) { cout << "This is a palindrome" << endl; }
    else { cout << "This is not a palindrome" < endl; }
    return 0;
}

【问题讨论】:

  • 在你上次的 cout 中你忘记了一个 &lt; 字符应该是:else{cout
  • 编辑您的问题
  • 只要学会解释错误信息。你做了什么? (我想:什么都没有。)
  • 我确实试图解释它,但我一直在看 cout 的第一个

标签: c++ overloading operator-keyword palindrome


【解决方案1】:

我收到了上述错误

最后一个

<endl;

必须

<<endl;

请仔细阅读错误消息。它们给出了发生错误的行号和字符位置。

我可以发誓我看了 10 次代码。

如果你能完美地格式化你的代码,你会很快发现错误。例如

#include <iostream>
using namespace std;

int main() {
  cout << "Write a number to check if its palindrome" << endl;
  int a;
  cin >> a;
  int z = a;
  int b;
  int c;
  while (a > 0) {
    b = a % 10;
    c = (c * 10) + b;
    a = a / 10;
  }
  if (z == c) {
    cout << "This is a palindrome" << endl;
  } else {
    cout << "This is not a palindrome" < endl;
  }
  return 0;
}

为什么它告诉我无论我做什么,这个数字都不是回文 放进去会很有用

你没有在循环之前用零初始化c

【讨论】:

  • 谢谢,我可以发誓我看了 10 次代码。我想我需要更多地训练自己来寻找那些愚蠢的错误
  • 您不应在问题标题前加上 (ANSWERED)。您应该标记对您有帮助的答案。
  • 好的,麻烦您了。我已经修好了。如果我犯了错误,我对这一切都很抱歉:/
猜你喜欢
  • 2019-04-09
  • 2021-05-21
  • 2011-12-10
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
相关资源
最近更新 更多