操作员过载的正确方法是什么,为什么这不起作用

What is the proper way of operator overloading and why this is not working?

本文关键字:是什么 为什么 不起作用 方法 操作员      更新时间:2023-10-16

我正在尝试重载=, + +=运算符,前两个工作正常,但operator+=()函数产生错误。
这是这三个的代码:

重载"="运算符

inline vec3& operator = (vec3& ob) { 
    mX = ob.getX();
    mY = ob.getY();
    mZ = ob.getZ();
    return *this;
}

重载"+"运算符

vec3 vec3::operator + (const vec3& ob) {
    vec3 vec(mX + ob.getX(), mY + ob.getY(), mZ + ob.getZ());
    return vec;
}

重载"+="运算符

vec3& vec3::operator += (const vec3& obj) {
    *this = *this + obj;
    return *this;
}

错误

binary '=': no operator found which takes a right-hand operand of type 'vec3'

值运算符应为常量:

vec3& operator= (const vec3& ob)