template <class T>
class MyLinkedList {
public:
class MyElement {
public:
T data;
MyElement *next;
} *head, *tail;
MyLinkedList () {
tail = head = NULL;
}
~MyLinkedList () {
// delete all elements in the list
//
for (MyElement *p = head; p; p = head) {
head = head->next;
delete p;
}
}
void Append(const T& data) {
if (!tail) {
// if the list is empty, insert one element
//
tail = head = new MyElement();
} else {
// insert one element at the end
//
tail->next = new MyElement();
// make the tail point to the last element
//
tail = tail->next;
}
// mark the last element as being 'last'
//
tail->next = NULL;
//
tail->data = data;
}
// overloading output operator
//
template <class T>
friend std::ostream& operator<< (std::ostream&, const MyLinkedList<T>&);
};
However, when I try to use the same code with another templated class:
1 2 3 4 5 6
template <class T>
class MyDatum {
T *data;
public:
MyDatum () : data(NULL) {}
};
And I try to instantiate the list as:
1 2 3
void main () {
MyLinkedList< MyDatum<char> > dList;
}
I get the following exception:
error C2040: 'dList' : 'MyLinkedList<T>' differs in levels of indirection from 'MyLinkedList<T> *'