1
class Rectangle : public Shape {

protected:
    int width;
    int length;

public:
    Rectangle(const int pwidth, const int plength, const int px, const int py);
    Rectangle& operator+=(Rectangle& rhs){
        this->width +=  rhs.width;
        this->length+= rhs.length;
        this->getX() += rhs.getX();
        this->getY() += rhs.getY();
        return *this;
    }
};

I am trying to overload the += operator in my rectangle class(subclass) and I am running into an issue when I try to bring the x and y values from my shape class(superclass) my idle is telling me the expression must be a modifiable lvalue what am I doing wrong? I have getters and setters in my shape class which do the obvious and they are both ints.

The errors occur on these lines

this->getX() += rhs.getX();
this->getY() += rhs.getY();
JJaco
  • 61
  • 1
  • 11
  • The parameter should be a `const` reference, and not a mutable reference. – Sam Varshavchik Nov 09 '20 at 02:55
  • The problem is in the assignment of the lvalue. – Suthiro Nov 09 '20 at 02:57
  • 1
    If `getX()` and `getY()` return by value, rather than by (non-`const`) reference, they cannot be used on the left hand side of an assignment. Although you haven't provided relevant information (tut-tut) it is possible to infer from the compiler diagnostics that they do not return a non`-const` reference. If the base provides accessible setters and getters like you claim, one option may be to do `this->setX(this->getX() + rhs.getX())`. (In most contexts, the `this->` is also not required, as it is the default behaviour). – Peter Nov 09 '20 at 03:06

0 Answers0