简体   繁体   中英

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"

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). 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). Just remove the line template<class T> from the member declaration.

Btw, you may want to consider having node a nested type of 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM