1
4

3 回答 3

9

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

于 2013-09-19T19:06:43.323 回答
3

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;
}
于 2013-09-19T19:12:00.317 回答
1

You should make the overloaded operator return a reference. Then it should work.

overload& operator=(const overload& o) {
  //do my assignment
  return *this;
}
于 2013-09-19T19:06:38.743 回答