Problem with Template Classes

My code looks like this:
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
template <typename T, typename R>
class LinkedList{
private:
	class Node{
	public:
		R data;
		Node* next;
		Node(const R& el): data(el), next(NULL){}
	}; // end Node class definition
	
	int size;
	Node* first;
	Node* findptr(const T&);
public:
	LinkedList();
	~LinkedList();
	void insert(const R&);
	bool find(const T&, R&);
	bool erase(const T&);
	void display();
}; // end LinkedList class definition

template <typename T, typename R>
LinkedList<T, R>::Node* LinkedList<T, R>::findptr(const T& key){
	Node* ptr = first;
	while(ptr!=NULL){
		if (ptr->data == key)
			return ptr;
		ptr = ptr->next;
	}
	return NULL;
}


The problem is with LinkedList<T, R>::Node*. For some reason the compiler really doesnt like the "*", but I have used code like this before just without templates and it worked just fine. Help?

Error: expected constructor, destructor, or type conversion b
efore '*' token
Error: expected `;' before '*' token
Last edited on
Thanks I have been looking around for a while now. After reading some of that book you found, I don't think I will ever use templates again. They just seem so confusing now.

Edit: Okay after reading that chapter, I still can't figure out how to fix my code and get rid of that error. I changed my code back to how it was originally and also I would like to note that the non-template version of this works fine for some reason:
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
typedef std::string KeyType; // makes KeyType a synonym for std::string

struct Article{
string title, author; 
KeyType key;
};

typedef struct Article ElementType; // makes ElementType a synonym for struct Article

class LinkedList{
private:
	class Node{
	public:
		ElementType data;
		Node* next;
		Node(const ElementType &el): data(el), next(NULL){}
	}; // end Node class definition
	
	int size;
	Node* first;
	Node* findptr(const KeyType &key);
public:
	LinkedList();
	~LinkedList();
	void insert(const ElementType&);
	bool find(const KeyType&, ElementType&);
	bool erase(const KeyType&);
	void display();
}; // end LinkedList class definition

LinkedList::Node* LinkedList::findptr(const KeyType &key){
	Node* ptr = first;
	while(ptr!=NULL){
		if (ptr->data == key)
			return ptr;
		ptr = ptr->next;
	}
	return NULL;
}


I can't see why the template one would not work... ::bangsheadonwall::

Edit #2: I decided to just ditch the templates for this program, but it would be beneficial if anyone can tell me what is wrong for future reference. Thanks in advance.
Last edited on
As was pointed out:

1
2
3
4
5
6
7
8
9
10
11
// note the "typename"
template <typename T, typename R>
typename LinkedList<T, R>::Node* LinkedList<T, R>::findptr(const T& key){
	Node* ptr = first;
	while(ptr!=NULL){
		if (ptr->data == key)
			return ptr;
		ptr = ptr->next;
	}
	return NULL;
}

Topic archived. No new replies allowed.