2015-09-04 55 views
-1

我尝试运行以下代码段时遇到了一些问题。我得到错误:“不能从const A转换参数到A”。智能感知告诉我A没有合适的拷贝构造函数,但是定义了一个。无法将参数从常量A转换为A

#include<iostream> 
using namespace std; 
class A 
{ 
    int x, *y; 
public: A(int i) { x = i; y = new int[x]; } 
     A(A& a) { x = a.x; y = new int[x]; } 
     int get_x() const { return x; } 
}; 

int f(A a) { return a.get_x(); } 
int main() { 
    const A a(5); 
    cout << (a.get_x() == f(a)); 
    system("Pause"); 
} 
+0

它说“no * suitable * copy constructor”是fou ND。你的不适合复制'const'对象。 – molbdnilo

回答

0

在构建temprary呼叫

int f(A a) { return a.get_x(); } 

你需要建立一个Aconst A,但构造函数需要一个A &

2

你的拷贝构造函数应具备原型:

A(const A & a) 
相关问题