Ryan
Ryan

Reputation: 35

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

I am implementing a doubly linked list.

I have 3 typedef statements in my class.

When compiled I get the error "'reference' does not name a type". If I move them under my include statements, this error disappears but new ones arise, such as 'Node' not being declared in my main scope. I am not sure where each of these typedef statements need to go in order to get my program functioning properly. I have included the default constructor as well.

#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;
}

...

Upvotes: 1

Views: 190

Answers (1)

Eugene
Eugene

Reputation: 7333

I have a partial answer for you: if your reference is defined inside the class, you need to include class name in the return type of the member function definitions when they are defined outside the class. E.g.

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

I cannot guess why Node is also a problem because you don't show any code using it.

Upvotes: 1

Related Questions