As others have mentioned, using a template without any reason to do so is neither standard nor recommended. However assuming you want operator==
to be more like one of the default comparison operators you should either mark it const
(otherwise you cannot compare two const Test
) like so
class Test {
public:
Test();
virtual ~Test();
int x,y;
bool operator==(const Test &v) const;
};
// MyTest.cpp file
bool Test::operator==(const Test &v) const {
return (x== v.x) && (y==v.y);
}
, or make it a free function (in the same namespace for ADL) taking two const Test &
(preferred) like so
class Test {
public:
Test();
virtual ~Test();
int x,y;
};
bool operator==(const Test &a, const Test &b);
// MyTest.cpp file
bool operator==(const Test &a, const Test &b) {
return (a.x==b.x) && (a.y==b.y);
}
If any access to private members is required the free function can always be marked friend
. In simple cases such as the one in this example it can be advantageous to furthermore define it in the header and marking it inline
.