Reputation: 239
#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;
}
I have the code above and the compiler is throwing incomprehensible errors. I can't quite figure out how to overload the == operator in a struct.
Upvotes: 2
Views: 6750
Reputation: 1310
The function signature should be const:
bool operator== (const coord &c1) const /* <--- */ {
return (x == c1.x && y == c1.y);
}
Upvotes: 0
Reputation: 206929
Add a pair of parens:
cout << ( xy1 == xy2 ) << endl;
otherwise this is parsed as:
(cout << xy1) == xy2
Upvotes: 3