请看下面的代码:
有人能解释为什么会返回以上错误吗?
"
如何修复上述错误(不创建全局二元运算符+或接受int的构造函数),以使
考虑以下代码:
class A
{
public:
A& operator=( const A& );
const A& operator+( const A& );
const A& operator+( int m );
};
int main()
{
A a;
a = ( a + a ) + 5; // error: binary '+' : no operator found which takes a left-hand operand of type 'const A'
}
有人能解释为什么会返回以上错误吗?
"
( a + a )
" 调用了 "const A& operator+( const A& )
" 并返回一个常量引用,然后如果我没弄错的话就传递给了 "const A& operator+( int m )
"。如何修复上述错误(不创建全局二元运算符+或接受int的构造函数),以使
main()
内的语句被允许?