使用g++ test.cpp
编译时,以下代码在Linux上引发错误:
#include <iostream>
using namespace std;
class A
{
public:
A(){
cout << "call A()" << endl;
};
A& operator = (const A& a) {
cout << "call operator =" << endl;
return *this;
}
A(A& a) {
cout << "call A(A& a)" << endl;
}
};
A operator - (A& a1, A& a2)
{
cout << "call operate -" << endl;
return a1;
}
int main()
{
A a1;
A a2;
A a3 = a1 - a2;
//a1 = a2;
return 0;
}
错误是:
test.cpp: In function ‘int main()’:
test.cpp:30: error: no matching function for call to ‘A::A(A)’
test.cpp:15: note: candidates are: A::A(A&)
test.cpp:8: note: A::A()
但是在使用Visual Studio 2010进行编译时,它适用于Windows。为什么?我在Linux上的代码出了什么问题?