1

I'm creating a doubly linked list implementation of my own for fun and I've finished writing the list itself but now I'm trying to add an iterator to it, but I'm confused about the syntax rules for this stuff. Here is the error I'm getting:

error C2990: 'd_list' : non-class template has already been declared as a class template

Here's the header file it's all located in:

/****************************/
/* d_list class             */
/* with exceptions classes  */
/****************************/
class d_list_error{};
class d_list_empty{};

template <class T>
class d_list_iter;

template <class T>
class d_list{
public:
    d_list();
    d_list(const d_list &_dl);
    d_list &operator=(const d_list &_dl);
    ~d_list();

    void push_back(T item);
    void push_front(T item);

    void pop_back();
    void pop_front();

    bool isEmpty() const;
    unsigned int size() const;

    void clear();
private:
    struct node{
        node *prev; 
        node *next;
        T    data;
    };
    node             *head;
    node             *tail;
    unsigned int     currSize;
    d_list_iter<T>   *dli;

    /* Utility Functions */
    void initFirstEle(T item, node* first);
    void copyAll(const d_list<T> &_dl);
};

/*************************/
/* d_list iterator class */ 
/*************************/

class d_iter_already_exists {};

template <class T>
class d_list_iter{
public:
    d_list_iter(const d_list &_dl);
    ~d_list_iter();
    T getNext() const;

private:
    friend class d_list;
    d_list<T>   *dl;
    node        *next;
    node        *prev;
    bool        valid;
};

Underneath that is all the member functions defined for d_list. I haven't started writing them for the iterator yet. d_list is working perfectly based on all my testing. I imagine I'm just running into syntax errors, as this is sort of uncharted territory for me.

3
  • At least for a first effort, it's usually easiest to write the iterator class nested inside the class it iterates. Commented Apr 24, 2012 at 4:00
  • That would have been smart... Commented Apr 24, 2012 at 4:05
  • Well… it can be done either way. Having the iterator outside actually helps enable the container to work with incomplete types. Also, the iterator class needs to be a template even if it is nested, because you want an extra parameter to differentiate iterator from const_iterator. Commented Apr 24, 2012 at 4:36

1 Answer 1

3

you must specify the templated type by specifying the template parameters outside the class declaration.

specifically, you should write (something like) d_list<T> instead of simply d_list within template <typename> class d_list_iter's declaration. this includes your use of friend.

same applies for the nodes. assume they are public for demonstration:

typename d_list<T>::node instead of simply node within template <typename> class d_list_iter's declaration.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.