overloading the = operator

I'm getting this error:
error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'const Name' (or there is no acceptable conversion)
1> could be 'built-in C++ operator!=(Name *, Name *)'

Here is my code:
const Name& Name::operator=(const Name& right)
{
if(this != right)
copy(right);
return *this;
}

Name contains const char* for a last name and a first name

I don't know what's going on, I have to check to see if it's not equal or self assigned...
It's complaining about the use of !=, which you have in that operator and probably have not yet defined.
To check if there's a self assignment issue you could do:
(this != &right)

To check if they are not equal you should write:
(*this != right)

In your example you missed the asterisk (*). But then again tummychow is right, you must also provide a definition for the != operator. (or you could define the == operator and do the checking like !(*this == right))
Last edited on
Checking the addresses of the object is sufficient. In the original code you had a mismatch.

1
2
3
// Here you are testing the address of the left hand side with an object on the right hand side.
// defining an operator != would not do you any good in that case.
if(this != right)


Instead just use m4ster r0shi's first example and forget that anyone said anything about defining an operator== or operator!= for your class.
if(this != &right)
Last edited on
Also take a look at this for more info on the subject.
http://cplusplus.com/articles/jsmith1/
Topic archived. No new replies allowed.