c++operator-overloading

Overload double equal operator in struct?


#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.


Solution

  • Add a pair of parens:

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

    otherwise this is parsed as:

    (cout << xy1) == xy2