开发者

Expected constructor, destructor, or type conversion before '*' token Templated linked list

My problem is really similar to this. However, my implementation differs in that I'm using a tem开发者_运维技巧plated 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 typename keyword:

template <class T>
typename List<T>::ListNode* List<T>::find(int k)
{
    ...
}

This lets the compiler know that ListNode is a type. It is needed whenever you have a dependent name (i.e. one which depends on a template parameter) which is a type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜