tid*_*idy 1 c++ linux compiler-errors
我编译时,以下代码在Linux上引发错误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;
}
Run Code Online (Sandbox Code Playgroud)
错误是:
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()
Run Code Online (Sandbox Code Playgroud)
但是在使用Visual Studio 2010进行编译时,它适用于Windows.为什么?我在Linux上的代码出了什么问题?
在线
A a3 = a1 - a2;
Run Code Online (Sandbox Code Playgroud)
在这里,你已经减去了,a1并a2产生了一个临时值(技术上是一个prvalue).但是,您的复制构造函数需要非const左值引用:
A(A& a) { ... }
Run Code Online (Sandbox Code Playgroud)
C++标准不允许这样做:prvalues不能绑定到非const左值引用.您应该在复制构造函数中使用const引用参数:
A(const A& a) { ... }
Run Code Online (Sandbox Code Playgroud)
至于为什么这被Visual C++接受,这似乎是为了向后兼容而保留的语言扩展,Brian说.看到类似的 问题.