2012-04-16 147 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?