Use of template classes in other template classes

I'm having some issues doing that.

I'm trying to make a template class which holds a template list I made. The list consists of template nodes (another template class I made).

My problem is that in some places (not everywhere) the compiler says that I'm trying to access private members of the node's class.

For example, if I define my node like this:
1
2
3
4
5
6
7
8
9
10
template<class T>
class CoefNode {
private:
	T coef;
	int exp;
	CoefNode* next;
	CoefNode* prev;
public:
	// Cunstructor
};


And I use that class when I allocate new CoefNodes while adding items to the list (the template class is CoefList).
So for example, when I try to create the destructor, I wrote this:
1
2
3
4
5
6
7
~CoefList() {
	CoefNode<T>* pIterator = head;
	while (pIterator) {
		pIterator = pIterator->next;
		delete pIterator->prev;
	}
}


And then the compiler gives me these errors:
genpoly.H: In destructor `CoefList<T>::~CoefList() [with T = Complex]':
poly_main.C:40: instantiated from here
genpoly.H:13: error: `CoefNode<Complex>*CoefNode<Complex>::next' is private
genpoly.H:94: error: within this context
genpoly.H:14: error: `CoefNode<Complex>*CoefNode<Complex>::prev' is private
genpoly.H:95: error: within this context


What am I doing wrong?
Thanks.
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
template <typename T>
class CoefNode
{
        T coef;
        CoefNode *next, *prev, *head;

public:
        CoefNode()
        {
                next = prev = head = 0;
        }

        ~CoefNode()
        {
                CoefNode<T>* p = head;
                while (p)
                {
                        p = p->next;
                        delete p->prev;
                }
        }
};

int main()
{
        CoefNode<int> c;
        return 0;
}


It seems to compile ok under gcc. There is a problem with your cleanup algorithm. Have you considered what happens at the end of the list?
I'm not sure I understood. Can you elaborate?

And why doesn't it have any problems with this, for example?
1
2
3
4
5
6
7
CoefList(const CoefList& list) {
	CoefNode<T>* pIterator = list.head;
	while (pIterator) {
		ListInsert(pIterator->coef, pIterator->exp);
		pIterator = pIterator->next;
	}
}


Thanks.
1
2
3
4
5
6
7
8
9
10
template<class T>
class CoefNode {
private: //<---
	T coef;
	int exp;
	CoefNode* next;
	CoefNode* prev;
public:
	// Cunstructor
};
They are private, ¿what you don't get?
You could make your container friend or the node, or you could make your node public (as inner class)
1
2
3
4
5
6
7
8
9
10
11
class CoefNode{
friend class CoefList;
//...
};
//Or
class CoefList{
private:
  struct CoefNode{
//all public
  };
};

1
2
3
4
5
6
7
8
9
        ~CoefNode()
        {
                CoefNode<T>* p = head;
                while (p)
                {
                        p = p->next;
                        delete p->prev; //suicidal zombie
                }
        }
I understood the problem.
Thanks.
Topic archived. No new replies allowed.