rize
rize

Reputation: 859

C++ template class compilation error

I get the following compilation error:

main.cc: In function 'int main(int, char**)':¶
main.cc:200: error: no match for 'operator==' in 'rt1 == rt2'¶
triple.hh:124: note: candidates are: bool Triple<T1, T2, T3>::operator==(const    Triple<T1,T2, T3>&) [with T1 = int, T2 = int, T3 = int] <near match>¶
main.cc:27: note:                 bool operator==(const Special&, const Special&)¶

Although I have implemented the operator== overload as follows for my template class:

bool operator==(const Triple<T1, T2, T3>& another) {
    return (a == another.first() and b == another.second() and c == another.third());
}

For my template class:

template <typename T1, typename T2, typename T3>
class Triple

Do you know what the problem might be? Many thanks.

Upvotes: 0

Views: 119

Answers (1)

Jirka Hanika
Jirka Hanika

Reputation: 13529

Your boolean operator is declared as non-const. Fix it as follows in case rt1 is a const reference. Note the added const keyword.

bool operator==(const Triple<T1, T2, T3>& another) const {

Explanation: C++ has two basic syntaxes for overloading a comparison operator; a member operator with one other argument, or a static operator with two arguments. However, in both cases, you should make sure that both operands are const, with the respective syntaxes.

It is theoretically possible to supply different const and non-const versions of the operator that do subtly different things, so the compiler calls yours a near-match, but nevertheless not a match.

Upvotes: 1

Related Questions