【发布时间】:2012-09-30 21:59:47
【问题描述】:
在类声明中重载的运算符:
class Asdf{
operator float() const;
Asdf operator+(const Asdf&) const;
Asdf operator+(float);
}
int main()
{
Asdf object1, object2, object3;
//Receiving error: "more than one operator '+' matches these operands"
object1= object2 + object3;
_getch();
return 0;
}
错误:
:error C2666: 'Asdf::operator +' : 3 个重载有类似的转换 : 可能是 'Asdf Asdf::operator +(float)' :'Asdf Asdf::operator +(const Asdf &) const'当我删除与重载的float 转换运算符一起使用的所有转换时,代码可以正确编译。
【问题讨论】:
-
为什么首先需要两个不同的加号运算符?这似乎是令人惊讶和困惑的秘诀。
标签: c++ operator-overloading operator-keyword