2015-09-04 37 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) 
相關問題