2
#include <iostream>
using namespace std;

struct coord {
 int x;
 int y;
  bool operator== (const coord &c1) {
   return (x == c1.x && y == c1.y);
  }
};

int main() {
 coord xy1 = {12, 20};
 coord xy2 = {12, 20};
 cout << xy1 == xy2 << endl;
 return 0;
}

我有上面的代码,编译器抛出了难以理解的错误。我不太清楚如何在结构中重载 == 运算符。

4

1 回答 1

3

添加一对括号:

cout << ( xy1 == xy2 ) << endl;

否则解析为:

(cout << xy1) == xy2
于 2012-04-26T05:09:54.817 回答