2012-04-16 126 views
1

好了,所以這裏是我的頭文件(或至少它的一部分):類模板,預計構造函數,析構函數

template<class T> 

class List 
{ 
public: 
. 
: 
List& operator= (const List& other); 
. 
: 
private: 
. 
: 
}; 

,這裏是我的.cc文件:在

template <class T> 
List& List<T>::operator= (const List& other) 
{ 
    if(this != &other) 
    { 
     List_Node * n = List::copy(other.head_); 
     delete [] head_; 
     head_ = n; 
    } 
    return *this; 
} 

List& List<T>::operator= (const List& other)我收到編譯錯誤「&'令牌'之前的預期構造函數,析構函數或類型轉換。我在這裏做錯了什麼?

+0

模板類定義必須位於頭文件中。看看這個問題的解釋:http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – 2012-04-16 12:13:08

回答

3

返回類型List&不能在沒有模板參數的情況下使用。它需要是List<T>&

template <class T> 
List<T>& List<T>::operator= (const List& other) 
{ 
    ... 
} 


但請注意,你解決這個語法錯誤,甚至後,你就會有連接問題,因爲模板函數定義需要放置在頭文件。有關更多信息,請參閱Why can templates only be implemented in the header file?

相關問題