2014-09-24 81 views
-4

怎麼回事?這是它引用的功能。我試圖讓這個作爲一個拷貝構造函數引用類型的初始化無效?

template <class T> 
const queue<Base>& queue<T>::operator=(const queue<Base> &q){ 
// Doesn't need to copy if they are the same object 
if (this != &q){ 
    delete [] data; 

    length = q.length; 
    capacity = q.capacity; 
    front = q.front; 

    data = new T[capacity]; 

    for (int i = 0; i < capacity; i++){ 
     data[i] = q.data[i]; 
    } 
} 

return this; 
} 
+0

當您作爲關於錯誤消息的問題時,習慣上指定哪條線產生錯誤。我們應該如何知道哪一行是第23行(或第190行)? – AnT 2014-09-24 23:44:59

+0

爲什麼你需要一個賦值操作符?使用矢量而不是原始數組,默認的數組將自動工作。 – 2014-09-25 00:15:07

+0

請注意,'operator ='應該返回一個*非const *引用。 – cdhowie 2014-09-26 03:11:54

回答

2

工作,這是你的錯誤

return this; 

this是一個指針。您的operator =被聲明爲返回參考。指針不能轉換爲引用。這是錯誤信息告訴你的。

相關問題