将Typedef语句放入类链接链接列表中的C 错误

C++ error when putting typedef statements inside class for doubly linked list

本文关键字:链接 列表 错误 Typedef 语句      更新时间:2023-10-16

我正在实现双重链接列表。

我班上有3个Typedef语句。

编译时,我会收到错误"'引用'不命名类型"。如果我将它们移到我的含义语句中,则此错误消失了,但出现了新错误,例如在我的主范围中未声明"节点"。我不确定这些Typedef语句中的每一个都需要去哪里,以使我的程序正常运行。我还包括默认构造函数。

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class linkedlist
{
public:
    typedef int element_type;
    typedef element_type& reference;
    typedef const element_type& const_reference;
    linkedlist();
    ~linkedlist();
    bool empty() const;
    void clear();
    reference back();
    const_reference back() const;
    reference front();
    const_reference front() const;
    linkedlist& operator=(const linkedlist& l);
    void pop_back();
    void pop_front();
    void push_back(const element_type& x);
    void push_front(const element_type& x);
    void sort();
    explicit linkedlist(unsigned int n);
    void check() const;
    void rcheck() const;

private:
    struct Node
    {
        element_type elem;
        Node* next;
        Node* prev;
    };
    Node* head;
    Node* tail;
    int size;
};
linkedlist::linkedlist()
{
    head = new Node;
    tail = new Node;
    head->next = tail;
    head->prev = NULL;
    tail->next = NULL;
    tail->prev = head;
    size = 0;
}

...

我对您有部分答案:如果您的reference在类内定义,则需要在成员函数定义的返回类型中的类名中包含类名称。。例如

linkedlist::reference linkedlist::back(){/*...*/}

我无法猜测为什么Node也是一个问题,因为您不使用它显示任何代码。