[英]Hi, am trying to make a function that returns a sublist from a linked list in C++
Thanks in Advance. 提前致谢。 am using template class & i get an error saying "Error C2955 'List': use of class template requires template argument list" 我正在使用模板类,并且出现错误消息“错误C2955'列表':使用类模板需要模板参数列表”
This is my list Class 这是我的清单课
template <class T>
struct node{
T Data;
node<T> * prev;
node<T> * next;
};
template <class T>
class List
{
public:
node<T> * front;
node<T> * rear;
List();
virtual ~List();
bool isEmpty();
void insertFirst(T Data);
void insertBack(T Data);
void insertBefore(node<T> * before, T Data);
void insertAfter(node<T> * after, T Data);
int removeFirst();
int removeLast();
void removeBefore(node<T> * before);
void removeAfter(node<T> * after);
node<T> * find(T Data);
//void destroy();
void insertRangeBefore(node<T> * before, List<T> range);
void insertRangeAfter(node<T> * after, List<T> range);
void removeRange(node<T> * rangeFirst, node<T> * rangeLast);
template <class T>
List getSublist(node<T> * rangeFirst, node<T> * rangeLast);
};
I Want to Create an instance of list and then return the sublist 我想创建一个列表实例,然后返回子列表
template<class T>
List<T> List<T>::getSublist(node<T>* rangeFirst, node<T>* rangeLast) {
return List();
}
template<class T>
List<T> List<T>::getSublist(node<T>* rangeFirst, node<T>* rangeLast)
// ^^^ corrections here
{
return List();
}
The unqualified name of the template ( List
) is equivalent to the fully qualified name ( List<T>
) only inside its definition (and those of its members). 仅在模板定义(及其成员的定义)内部,模板的非限定名称( List
)等同于完全限定名称( List<T>
)。 However, the return type here is neither, so must be fully qualified. 但是,此处的返回类型都不是,因此必须完全限定。
Edit Your header file does actually not declare this member function and your code will still not compile. 编辑您的头文件实际上并未声明此成员函数,并且您的代码仍将无法编译。 You must provide a matching declaration , ie 您必须提供匹配的声明 ,即
template<class T>
class List
{
...
List getSublist(node<T>*, node<T>*); // not a template
};
Your code actually declared a templated member (with another type T
as template parameter). 您的代码实际上声明了一个模板成员(带有另一个T
类型作为模板参数)。 Just remove the line template<class T>
from the member declaration. 只需从成员声明中删除行template<class T>
。
Btw, you may want to consider having node
a nested type of List<T>
. 顺便说一句,您可能要考虑让node
使用List<T>
的嵌套类型。 Then you can replace node<T>
with node
(almost) everywhere (except when you must qualify it, when it is List<T>::node
or typename List<T>::node
). 然后,您可以在所有地方(几乎)用node
(几乎)替换node<T>
(除非您必须限定它的资格,当它是List<T>::node
或typename List<T>::node
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.