我遇到语法错误,我确定是正确的:

expected constructor, destructor, or type conversion before '*' token
expected `;' before '*' token

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

    ListNode* find(int index) const;
    ......
}

列表文件
template <typename T>
ListP<T>::ListNode* ListP<T>::find(int index) const
{
 ......
}

错误发生在该行。
ListP<T>::ListNode* ListP<T>::find(int index) const

最佳答案

看来您有3个问题:

类定义后缺少分号:

};

缺少typename:
typename ListP<T>::ListNode* ListP<T>::find(int index) const

有关更多信息,请参见Where and why do I have to put the “template” and “typename” keywords?

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

有关详细说明,请参见Why can templates only be implemented in the header file?

08-17 03:10