My problem is really similar to this. However, my implementation differs in that I’m using a templated linked-list
Here’s where I’m getting errors (second line). The purpose of this function is to return a pointer to a node at the k-th location in the list
template <class T>
List<T>::ListNode* List<T>::find(int k)
{
ListNode * curr = head;
while(curr != NULL && k > 0) {
curr = curr->next;
k--;
}
return curr;
}
And this is what my list looks like (made up of nodes, which store arbitrary data of type T)
template <class T>
class List
{
private:
class ListNode
{
public:
ListNode();
ListNode(T const & ndata);
ListNode * next;
ListNode * prev;
const T data;
};
Essentially this is the same question as the one I linked to, except that my list is templated. So, after making the changes that fixed the other problem, my code still throws errors. Any ideas as to why this is happening?
You need to use the
typenamekeyword:This lets the compiler know that
ListNodeis a type. It is needed whenever you have a dependent name (i.e. one which depends on a template parameter) which is a type.