pro*_*bie 0 c++ operator-overloading
我重写了添加操作,这样我就可以添加我的struct Vec3的两个向量
// addition
Vec3 operator+(Vec3<T> &other) {
return Vec3(this->x + other.x, this->y + other.y, this->z + other.z);
}
// product with one scalar
Vec3 operator*(float scalar) {
return Vec3(this->x * scalar, this->y * scalar, this->z * scalar);
}
Run Code Online (Sandbox Code Playgroud)
Vec3只有T型的三个属性.
使用它时T是浮点数,我执行此代码:
vec temp = vecOne * skalarOne + vecTwo * scalarTwo;
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
二元运算符'+'不能应用于'pray :: Vec3'和'pray :: Vec3'类型的表达式
如果首先计算乘法并将结果保存在向量中然后执行向量加法,则不会出现此错误.
有人有什么想法?谢谢!
| 归档时间: |
|
| 查看次数: |
35 次 |
| 最近记录: |