The following code raises an error on Linux when I compile with g++ test.cpp
:
#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;
}
The error is:
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()
But it works on Windows when compiling with Visual Studio 2010. Why? What's wrong with my code on Linux?