2012-08-10 130 views
4

我在模板上工作類似於java中的ArrayList的C++類(是的,我知道矢量做同樣的事情,這不是一個功利的編碼項目)。新的C++模板類構造函數,它本身的類型作爲參數

我覺得爲我的ArrayList類使用另一個ArrayList作爲種子ArrayList的參數將會很有用。但是當我嘗試寫構造函數時出現這個錯誤

invalid constructor; you probably meant 'ArrayList<T> (const ArrayList<T>&)' 

這是否意味着ArrayList必須是常量?爲什麼我需要運營商的地址?

我仍然在學習C++的繩索,所以我有點困惑。

的原型在這裏:

ArrayList(ArrayList<T> list); 
    ArrayList(ArrayList<T> list, int size); 

的代碼是在這裏:

/** 
* Creates an ArrayList of type T that is twice the 
* size of the passed in ArrayList, and adds all elements 
* from the passed ArrayList<T> list, to this ArrayList. 
* 
* Runs in O(n) time, where n = the size of list. 
* 
* @param list the ArrayList to use as a seed for this ArrayList. 
*/ 
template<class T> 
ArrayList<T>::ArrayList(ArrayList<T> list) { 

    array = new T[list.getSize() * 2]; 
    capacity = list->getSize() * 2; 
    size = list->getSize(); 

    for (int i = 0; i < list->getSize(); i++) { 

     array[i] = list->get(i); 
    } 
} 

編輯 下面的代碼變得沒有錯誤,而上面確實.....

/** 
* Creates an ArrayList of type T that has a capacity equal to the passed 
* in theCapacity parameter. This ArrayList starts with the passed ArrayList. 
* 
* Note: If the passed in capacity is smaller than the size of the passed in 
*   ArrayList, then the capacity is set to twice the size of the 
*   passed ArrayList. 
* 
* @param list the ArrayList to use as a seed for this ArrayList. 
* @param theCapacity the capacity for this ArrayList. 
*/ 
template<class T> 
ArrayList<T>::ArrayList(ArrayList<T> list, int theCapacity) { 

    if (theCapacity >= list->getSize()) { 

     array = new T[theCapacity]; 
     capacity = theCapacity; 
    } 
    else { 

     array = new T[list->getSize() * 2]; 
     capacity = list->getSize() * 2; 
    } 

    size = list->size; 

    for (int i = 0; i < size; i++) { 

     array[i] = list->get(i); 
    } 
} 
+0

請發佈一個完整的,最簡單的例子 – 2012-08-10 02:30:34

+0

哦,狗屎我忘了點擊ctrl + C – Ethan 2012-08-10 02:31:03

+0

你正試圖編寫一個拷貝構造函數,它必須有一個引用參數。 http://stackoverflow.com/questions/2685854/why-should-the-copy-constructor-accept-its-parameter-by-reference-in-c – aschepler 2012-08-10 02:35:55

回答

3

使用

ArrayList<T>::ArrayList(const ArrayList<T>& list) 

作爲您的構造函數簽名將arraylist作爲const引用傳遞。這是複製構造函數的正確簽名。執行和調用代碼都不需要改變,除非你在你的ctor中修改list

當您執行ArrayList<T>::ArrayList(ArrayList<T> list)時,您正在創建整個ArrayList實例的臨時副本。 (你不能爲ctors做這件事,因爲它會調用無限遞歸,因爲list的新副本將使用相同的函數定義,等等)。

ArrayList<T>::ArrayList(ArrayList<T>& list)傳遞列表作爲參考,這意味着它不再是通常所謂的「按值傳遞」,並且從調用代碼處理列表的確切版本。

通過在一個函數中接受一個const引用,就是說函數不會修改引用的內容(即不對其進行任何修改操作:它將僅限於const訪問)。在繼續閱讀之前,您應該閱讀有關const,引用和複製構造函數。

更新:

對於由值或基準通行,可以通過obj.member語法訪問成員。如果您傳遞的是像ArrayList<T>::ArrayList(ArrayList<T>* list)這樣的指針,則必須使用list->member語法或(*list).member語法,更別提先檢查列表是否爲0/nullptr(您無法取消引用空指針)。

您正在爲指針和語法的值/引用混合使用語法。由於您未使用指針傳遞list參數,因此將所有list->x轉換爲list.x

爲不同的通行爲參見本:http://ideone.com/3c5mJ

+0

當我切換到,雖然我得到這個錯誤的內容構造函數 ' - >'的基本操作數具有非指針類型'const ArrayList ' 如果我使用addressof它怎麼可能是非指針類型? :? – Ethan 2012-08-10 02:39:54

+0

@現在就是它的一個引用,你可以像傳遞值一樣使用'obj.member'語法。這是參考如何在C++中工作。 – 2012-08-10 02:43:59

+0

這意味着你在實現你的函數時遇到了一些問題。你需要給我們更多的細節,你如何實現這個類並使用它。 – klm123 2012-08-10 02:45:15

2

「常量」是因爲它是一些對象,你會不會改變的參考。使用 引用,因爲這是複製構造函數,默認語法假設引用。

但最重要的是:您可以以與您嘗試編寫的完全相同的方式定義和使用此構造函數。

相關問題