#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;
}
我有上面的代码,编译器抛出了难以理解的错误。我无法弄清楚如何在结构中重载==运算符。
答案 0 :(得分:3)
添加一对parens:
cout << ( xy1 == xy2 ) << endl;
否则将其解析为:
(cout << xy1) == xy2