C ++如何在子类和超类中重载+ =

问题描述

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;
    }
};

我试图重载矩形类(子类)中的+ =运算符,当我尝试从形状类(超类)中获取x和y值时遇到一个问题,我的空闲状态告诉我表达式必须是可修改的左值我在做什么错?我的shape类中有一些getter和setter方法,它们很明显,而且它们都是int。

这些行上发生错误

this->getX() += rhs.getX();
this->getY() += rhs.getY();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)