为List编写查找函数时出现语法错误

1 c++ syntax list

我遇到了一个语法错误,我确信它是正确的:

expected constructor, destructor, or type conversion before '*' token
expected `;' before '*' token 
Run Code Online (Sandbox Code Playgroud)

ListP.h

#ifndef LISTP_H
#define LISTP_H
template <typename T>
class ListP
{
private:
    struct ListNode
    {
        T item;
        ListNode* next;
    };

    ListNode* find(int index) const;
    ......
}
Run Code Online (Sandbox Code Playgroud)

ListP.cpp

template <typename T>
ListP<T>::ListNode* ListP<T>::find(int index) const
{
 ......
}
Run Code Online (Sandbox Code Playgroud)

错误发生在该行.

ListP<T>::ListNode* ListP<T>::find(int index) const
Run Code Online (Sandbox Code Playgroud)

Jes*_*ood 5

看起来你有3个问题:

课程定义后缺少分号:

};
Run Code Online (Sandbox Code Playgroud)

遗失typename:

typename ListP<T>::ListNode* ListP<T>::find(int index) const
Run Code Online (Sandbox Code Playgroud)

看看我在哪里以及为什么要放置"template"和"typename"关键字?了解更多信息.

你应该在头文件中实现模板

请参阅为什么模板只能在头文件中实现?为了一个很好的解释.