gpt4 book ai didi

c++ - CPP : Overloaded nested operator not working

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:33 24 4
gpt4 key购买 nike

我重写了加法运算,这样我就可以将我的 struct Vec3 的两个 vector 相加

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

Vec3 只有三个类型 T 的属性。

使用时 T 是一个 float ,我执行这段代码:

vec temp = vecOne * skalarOne + vecTwo * scalarTwo;

我收到这个错误:

Binary operator '+' can't be applied to the expressions of type 'pray::Vec3' and 'pray::Vec3'

如果先计算乘法并将结果保存在 vector 中,然后再进行 vector 加法,我就不会收到此错误。

有人知道吗?谢谢!

最佳答案

您需要将函数签名更改为

Vec3 operator+(const Vec3<T> &other) const

&c.,否则匿名临时无法绑定(bind)到它。

关于c++ - CPP : Overloaded nested operator not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com