Reputation: 2431
I have a template class with three template types (basically a pair with one more member) and I can't get the comparison overload to work:
header:
template<typename FirstType, typename SecondType, typename ThirdType>
class Triple
{
public:
Triple(FirstType f, SecondType s, ThirdType t) : var1(f), var2(s), var3(t)
{}
...
private:
FirstType var1;
SecondType var2;
ThirdType var3;
};
template<typename F1, typename S1, typename T1, typename F2, typename S2, typename T2>
bool operator==(const Triple<F1,S1,T1>& one, const Triple<F2,S2,T2>& other)
{
return true; //just for testing
}
template<typename F1, typename S1, typename T1, typename F2, typename S2, typename T2>
bool testFunc(const Triple<F1,S1,T1>& one, const Triple<F2,S2,T2>& other)
{
return true; //just for testing
}
main:
Triple<int, int, int> asdf(1, 1, 2);
Triple<int, int, int> qwer(1, 21, 2);
cout << asfd == qwer; //doesn't work
cout << testFunc(asfd, qwer); //works
I get the following error message with the == operator:
binary '==' : no operator found which takes a right-hand operand of type 'Triple<FirstType,SecondType,ThirdType>'
Why testFunc works but the operator overload doesn't? Note that I want to include the possibility of comparing two different types of Triples, since someone might want to compare ints with doubles, for instance. I also tried implementing the comparison function inside the class with same results.
Upvotes: 1
Views: 2604
Reputation: 10770
<<
has a higher precedence than ==
. This means your expression is parsed as
(cout << asdf) == qwer;
Just add brackets to fix this
cout << (asdf == qwer);
Upvotes: 5