链接运算符时运算符重载失败



我用以下重载运算符创建了一个自定义可变长度向量类Vec

float& operator[](int i);
Vec& operator+=(Vec& rhs);
Vec operator+(Vec& rhs);
Vec& operator-=(Vec& rhs);
Vec operator-(Vec& rhs);
Vec& operator*=(float rhs);
Vec operator*(float rhs);
Vec& operator/=(float rhs);
Vec operator/(float rhs);

这些重载单独运行很好,我得到了正确的结果,但当我尝试将它们链接时,我会使用template argument deduction/substitution failed出现编译错误。有人知道为什么吗?

这项工作:

Vec multiplier = d * time;
Vec collision = e + multiplier;

此操作失败:Vec collision = e + (d * time);

e和d属于Vec型,时间属于float

问题是,您已将函数声明为接受Vec &rhs参数,而不是const Vec &rhs参数。缺少const意味着您只能传递对真实(命名(对象的引用;不能将引用传递给未命名的临时对象。

当你"连锁"操作时,内部操作的结果将是一个未命名的临时操作,然后无法将其传递给外部操作,因此你会得到过载解决失败(表现为模板替换失败,因为你的类实际上是一个模板(

最新更新