using template within the template

Hi there, I have a question about templates. I have written the following implementation of linked list which works fine for the primitive types:

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
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> *'

Any ideas what am I doing wrong here?

Thanks!!
Last edited on
gcc complains on
1
2
template <class T>
friend std::ostream& operator<< (std::ostream&, const MyLinkedList<T>&);
as you are using the same template parameter `T'.
Also, main must return int.

After that, it compiles. So I suggest you to change your compiler.
Wow.. I would never have thought Visual Studio 2010 compiler could be an issue.. Thanks!
Topic archived. No new replies allowed.