首页 > 解决方案 > 错误:预期的类名(链表 C++)

问题描述

我应该创建一个链接列表(要求现在将所有内容放在头文件中)并且我正在尝试为节点创建一个结构,但它说它需要一个类名。我做错了什么?我对使用结构为列表创建节点有点困惑。

#include "LinkedListInterface.h"

#ifndef LAB03LINKEDLIST_LINKEDLIST_H
#define LAB03LINKEDLIST_LINKEDLIST_H


template <typename T>
class LinkedList: public LinkedListInterface<T>
{
public:    // this is where the functions go
    LinkedList();
    void AddNode(T addData)
    {
        nodePtr n = new node;
        n->next = NULL;
        n->data = addData;

        if (head != NULL)
        {
            current = head;
            while (current->next != NULL)
            {
                current = current->next;
            }
            current->next = n;
        }
        else
        {
            head = n;
        }
    }
    void DeleteNode(T delData);
    void PrintList();
private:
    struct node:
    {
    T data;
    node* next;
    };
    typedef struct node* nodePtr;
    nodePtr head;
    nodePtr current;
    nodePtr temp;
};



#endif //LAB03LINKEDLIST_LINKEDLIST_H

标签: c++structlinked-listnodes

解决方案


struct node:
{
    T data;
    node* next;
};

应该

struct node
{
    T data;
    node* next;
};

:除非您打算使用继承,否则类名后面没有- @john


推荐阅读