2

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?

7
  • 6
    The error message you posted says your get() function doesn't return const Type<Colors>* but rather Node<Key_T,Value_T>*. Are you calling get() on the appropriate object? Commented Jul 28, 2011 at 8:23
  • 1
    what is the definition of get? Commented Jul 28, 2011 at 8:24
  • 1
    Could it be just the difference in costness? That's the only thing I see. Commented Jul 28, 2011 at 8:24
  • 3
    look at the error: it looks to the compiler that get() returns Node<int,int>! Commented Jul 28, 2011 at 8:27
  • 1
    you should give some more details about your get() function. From the error message it seems the return type of get() function has some problems or may be your class Colors doesn't have a proper operator==, but this is just a guess. Commented Jul 28, 2011 at 8:27

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

GCC 4.5.2 and operator ==() return value
@iammilind: fair point - I originally only declared the operator to prove it'd get a link error, then decided to whack a body on so I'd prove it completely compiled and linked... it continues to do so with a "return true;" thrown in (edited in now too).
0

The function operator== neither modify left or right ?

So put then const and it will works.

3 Comments

Wrong, the arguments are passed by value. No const needed.
As presented, it's a non-member function, so there's no object in respect of which the function can be const. Konrad's covered the possibility that you meant the function parameters themselves.
If the first argument is of type const Type<Colors>* it won't compile even if the function don't modify the pointer value. That's why he should put const where he can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.