inheritance, nested classes, templates?

I have an assignment for class where I have to implement a linked list and then create the derived classes for stack and queue. The linked list works, but I'm having trouble doing the inheritance for stack and queue. This is sample code of how I've set things up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
template <class T>
class NODE
{
private:
    NODE *prev;
    NODE *next;
    T data;
public:
    //functions...
};

template <class T>
class LIST
{
private:
    NODE *first;
    NODE *last;
    int size;
public:
    class ITERATOR
    {
    private:
        NODE *node;
    public:
        ITERATOR() : node(NULL) {}
        ITERATOR(NODE<T> *n) : node(n) {}
        ITERATOR(const ITERATOR &other)
        {
            node = other.node;
        }
        //other functions...
    };
    ITERATOR begin() { return ITERATOR(first); }
    ITERATOR end() { return ITERATOR(last->next); }
    //other functions...
};

class template <class T>
class STACK : public LIST<T>
{
public:
    class ITERATOR : public LIST<T>::ITERATOR
    {
        ITERATOR() : LIST<T>::ITERATOR() {}
        ITERATOR(NODE<T> *n) : LIST<T>::ITERATOR(n) {}
        ITERATOR(const ITERATOR &other)
        {
            this->setNode(other.getNode());
        }
        //other functions...
    };
    ITERATOR begin() { return ITERATOR(this->getFirst()); }
    ITERATOR end() { return ITERATOR(this->getLast()->getNext()); }
    //other functions...
};

class template <class T>
class QUEUE : public LIST<T>
{
public:
    class ITERATOR : public LIST<T>::ITERATOR
    {
        ITERATOR() : LIST<T>::ITERATOR() {}
        ITERATOR(NODE<T> *n) : LIST<T>::ITERATOR(n) {}
        ITERATOR(const ITERATOR &other)
        {
            this->setNode(other.getNode());
        }
        //other functions...
    };
    ITERATOR begin() { return ITERATOR(this->getFirst()); }
    ITERATOR end() { return ITERATOR(this->getLast()->getNext()); }
    //other functions...
};


I created a nested class called ITERATOR inside the LIST class to imitate the iterators in the STL containers. This works fine:
1
2
LIST<int> mylist;
for (LIST::ITERATOR it = mylist.begin(); it != mylist.end(); it++);


The following gives "error: 'STACK<T>::ITERATOR::ITERATOR(const STACK<T>::ITERATOR&) [with T = int; STACK<T>::ITERATOR = STACK<int>::ITERATOR]' is private" and points to the copy constructor of STACK::ITERATOR.
1
2
STACK<int> mystack;
for (STACK::ITERATOR it = mystack.begin(); it != mystack.end(); it++);


How do I fix this?

EDIT: Figured it out. Keep making stupid mistakes due to lack of sleep. I forgot to put in the access specifiers and before that I had forgotten to put in constructor functions.
Last edited on
Topic archived. No new replies allowed.