C++ 类模板,应为构造函数、析构函数
好的,这是我的头文件(或至少部分头文件):C++ 类模板,应为构造函数、析构函数,c++,class,templates,constructor,C++,Class,Templates,Constructor,好的,这是我的头文件(或至少部分头文件): 模板 班级名单 { 公众: . : 列表和运算符=(常量列表和其他); . : 私人: . : }; 这是我的.cc文件: template <class T> List& List<T>::operator= (const List& other) { if(this != &other) { List_Node * n = List::copy(other.head_
模板
班级名单
{
公众:
.
:
列表和运算符=(常量列表和其他);
.
:
私人:
.
:
};
这是我的.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\u Node*n=List::copy(其他.head\u);
删除[]标题;
头=n;
}
归还*这个;
}
在
List&List::operator=(const List&other)
行中,我得到了编译错误“预期的构造函数、析构函数或“&”标记之前的类型转换”。我在这里做错了什么?如果没有模板参数,就不能使用返回类型列表&
。它需要是列表&
模板
列表和列表::运算符=(常量列表和其他)
{
...
}
但请注意,即使修复了此语法错误,也会出现链接器问题,因为模板函数定义需要放在头文件中。有关详细信息,请参阅。模板类定义必须位于头文件中。请看此问题以进行解释:
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;
}
template <class T>
List<T>& List<T>::operator= (const List& other)
{
...
}