Reputation: 17128
I've run into strange problem with binary operator==
.
I have a function which returns:
Type< Colors >* get();
, T
is of type enum Colors {Red,Black}
and I have an operator==
defined as:
bool operator==(Type<Colors>* left, Colors right)
{
//...
}
Now, in code I have line:
if (get() == Red)
{
//
}
but here I'm getting error saying that:
error C2679: binary '==' : no operator found which takes a right-hand operand of type 'Colors' (or there is no acceptable conversion)
1> could be 'built-in C++ operator==(Node<Key_T,Value_T> *, Node<Key_T,Value_T> *)'
1> with
1> [
1> Key_T=int,
1> Value_T=int
1> ]
or 'bool operator ==(const Type<T> *,const Colors)'
1> with
1> [
1> T=Colors
1> ]
1> while trying to match the argument list '(Node<Key_T,Value_T> *, Colors)'
1> with
1> [
1> Key_T=int,
1> Value_T=int
1> ]
Obviously the second match is what I've intended to use and it's perfect match yet it doesn't want to ;) compile. What am I doing wrong?
Upvotes: 2
Views: 284
Reputation: 106206
(This is more diagnostic than an answer per se... but too much for a comment.)
Works ok for me with GGC 4.5.2:
enum Colour { Red, Black };
template <typename T>
struct Type { };
bool operator==(Type<Colour>*, Colour) { return true; }
int main()
{
Type<Colour>* p;
return p == Black;
}
Please try the above on your compiler and post the error message if any. If none, please post your EXACT complete program as the error is likely some subtle thing you haven't posted.
Upvotes: 2
Reputation: 1849
The function operator==
neither modify left or right ?
So put then const and it will works.
Upvotes: 0