3 回答
You're modifying a local object and returning it by value. You need to modify this
object
and return a reference to it:
overload& overload::operator=(const overload& ov)
{
this->x = ov.x;
this->y = ov.y;
return *this;
}
The error you get is because a temporary returned by a function cannot bind to a reference to non-const (hence the const
in my example).
The issue is that your assignment operator take a reference to non-const and returns by value. Since the value returned from the first assignment is treated as a temporary, the compiler won't bind a reference to it, so the second assignment fails. The usual form of an overloaded assignment operator is:
T &T::operator=(T const &other)
{
...
return *this;
}
You should make the overloaded operator return a reference. Then it should work.
overload& operator=(const overload& o) {
//do my assignment
return *this;
}