操作员过载的正确方法是什么,为什么这不起作用
What is the proper way of operator overloading and why this is not working?
我正在尝试重载=, + +=
运算符,前两个工作正常,但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)
相关文章:
- 为什么此指针值不能转换为整数的规则是什么?
- 这是什么代码?为什么它有效?C++
- scanf( "%d" , array[i] + 1)是什么意思?为什么+1在那里?
- 为什么左值不能改变自己。左值用户是什么意思?
- 抛出多个异常时,catch 块执行的顺序是什么,为什么?
- 使用大括号实例化 - 它是什么,为什么在这里使用它?
- 为什么BOOST :: HANA :: EXAREMIS ::键入实验功能?类型列表的棘手是什么
- 抽象类的需求是什么?为什么要通过其基类访问派生类方法?在C++
- for 循环后的分号是什么意思?为什么第一个循环语句中有 j++
- 操作员过载的正确方法是什么,为什么这不起作用
- 为什么我会遇到细分错误?将输出作为数字而不是字符串的原因是什么?
- 什么是bad_alloc?为什么输出屏幕没有显示所有值
- 具有多个非可选参数的转换构造函数是什么样子的?为什么它有意义
- 字符串数组[] = " " 是什么;意思是,为什么它有效?
- C++ 个字符数据类型的用途是什么?为什么不直接使用字符串?
- nan在C++中是什么意思?为什么pow(-4,-2.1)返回-nan
- 为什么铬执行时间::现在?好处是什么
- 在iOS中使用Objective-C背后的哲学是什么?为什么不使用c++呢?
- c++中的回调地狱是什么?为什么它会泄漏内存?
- LookupAddressForName的作用是什么?为什么它如此缓慢