简体   繁体   English

嗨,我正在尝试制作一个从C ++中的链接列表返回子列表的函数

[英]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>::nodetypename List<T>::node )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Java学习C ++,尝试创建链表 - Learning C++ from Java , trying to make a linked list 尝试在C ++中使用“无匹配&#39;operator =&#39;”” - “no match for 'operator='” in c++, trying to make a linked list 在创建此函数以从C ++中的单链表搜索元素时,我在哪里出错? - Where am I making mistake in creating this function to search element from singly linked list in C++? C ++ typelist make子列表 - C++ typelist make sublist 当我尝试在 c++ 中实现我的链表时出现奇怪的行为 - Strange behaviour when I am trying to implement my linked list in c++ 尝试以递归方式在 C++ 中打印链表 - Trying to print a linked list in c++ recursively 如何使链表在 C++ 中有序? - How to make the linked list ordered in C++? 如何使C++中的function从链表中删除与搜索键匹配的节点(节点可以是第一个节点以外的任何节点) - How to make a function in C++ to delete a node from the linked list (node can be any except the first node) which matches the searched key 尝试在C ++中创建一个单链接列表,该列表按时间顺序存储值,而不是向后存储所有内容 - Trying to make a single-linked list in C++ that stores values in chronological order instead of storing everything backwards 链表(C ++)中pop()函数的实现 - pop() function implementation in a Linked List (C++)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM