use typedef type as a function return type

726 views Asked by At

I tried to use a typedef type as a return type for my member function, I don't know why it is giving me this "LinkedList" is not a class error. Can someone please give me a hint?

template<class T>
class LinkedList {

    struct Node;
public:

    typedef Node* lstIterator;
    lstIterator insert_after(const_lstIterator position, const T& );
    // other code

private:
    struct Node {
        T data;
        Node *next;
        // default constructor
        Node() = default; 
        // constructor with data
        Node(const T& data) : data(data), next(NULL) {}
    };

    lstIterator head;
};


template<typename T>
LinkedList::lstIterator LinkedList<T>::insert_after(const_lstIterator position, const T& val) { 
 ^^^^^^^^^^^^^^^^^^^^^
error: "LinkdedList" is not a class, namespace or scoped enumeration
}
1

There are 1 answers

0
vsoftco On BEST ANSWER

Use

template<typename T>
typename LinkedList<T>::lstIterator 
LinkedList<T>::insert_after(const_lstIterator position, const T& val) 
{ 
    // implementation
}

instead, as LinkedList is a template class. You need typename since LinkedList<T>::lstIterator is a dependent name.