cannot solve this error...

Now I'm studying about writing template in LinkedList class...

This is my first time writing template class

There r a lot of bugs at the first time... but after googling, a lot of bugs decrease. However, i still have some problems in LinkedList.cpp

please check for me.

Thank you in advance

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
//LinkedList.h
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_


template <class T>
class LinkedList {
private:
	struct LinkNode {
		T item;
		LinkNode *next;
	};
	LinkNode *head;
	int _size;
public:

	LinkedList();
	~LinkedList();

	bool isEmpty() const;
	int getLength() const;
	
	LinkNode* TraverseTo(int index);

	bool addItem(T& newItem, int index);
	bool deleteItem(int index);
	T& retriveItem(int index);

	string toString();
};

#endif 


and

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//LinkedList.cpp
#include<iostream>
#include<sstream>
using namespace std;
#include"LinkedList.h"

template <class T>
LinkedList<T>::LinkedList() {
	head = NULL;
	_size = 0;
}

template <class T>
LinkedList<T>::~LinkedList() {
	while(!isEmpty())
		deleteItem(0);
}

template <class T>
bool LinkedList<T>::isEmpty() const {
	return _size==0;
}

template <class T>
int LinkedList<T>::getLength() const {
	return _size;
}

template <class T>
LinkedList<T>::LinkNode* LinkedList<T>::TraverseTo(int index) { /*error this line::
Error	4	error C1903: unable to recover from previous error(s); stopping compilation	c:\users\xxxxx\documents\course\cg1103\lab_cc\linkedlisttemplate\linkedlisttemplate\linkedlist.cpp	29
Error	2	error C2143: syntax error : missing ';' before '*'	c:\users\xxxxx\documents\course\cg1103\lab_cc\linkedlisttemplate\linkedlisttemplate\linkedlist.cpp	29
Error	3	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\xxxxx\documents\course\cg1103\lab_cc\linkedlisttemplate\linkedlisttemplate\linkedlist.cpp	29
*/
	if(index >= _size || index < 0)
		return NULL;
	else
	{
		LinkNode *cur = head;
		for(int i=0; i<index; i++)
			cur = cur->next;
		return cur;
	}
}


template <class T>
bool LinkedList<T>::addItem(T& newItem, int index) {
	if(index < 0 || index > _size)
		return false;
	else if(isEmpty() || index == 0) {
		LinkNode* temp = new Linknode;
		temp->item = newItem;
		temp->next = head;
		head = temp;
	}
	else{
		LinkNode *cur = TraverseTo(index-1);
		LinkNode *temp = new LinkNode;
		temp->item = newItem;
		temp->next = cur->next;
		cur->next = temp;
	}
	_size++;
	return true;
}

template <class T>
bool LinkedList<T>::deleteItem(int index) {
	if(index <0 || index >= _size) 
		return false;
	else if(index = 0){
		LinkNode *cur = head;
		head = head->next;
		delete cur;
	}
	else {
		LinkNode *cur = TraverseTo(index -1);
		LinkNode *temp = cur->next;
		cur->next = cur->next->next;
		delete temp;
	}
	_size--;
	return true;
}

template <class T>
T& LinkedList<T>::retriveItem(int index){
	LinkNode *cur = TraverseTo(index);
	return cur->item;
}


template <class T>
string LinkedList<T>::toString() {
	ostringstream os;
	os << this->item << endl;
	return os.str();
}
This:
1
2
3
4
template <class T>
typename LinkedList<T>::LinkNode* LinkedList<T>::TraverseTo(int index) { // typename keyword

// ... 

And this:
1
2
3
4
5
6
7
8
9
10
template <class T>
bool LinkedList<T>::addItem(T& newItem, int index) {
	if(index < 0 || index > _size)
		return false;
	else if(isEmpty() || index == 0) {
		LinkedList<T>::LinkNode* temp = new LinkedList<T>::LinkNode; // full typename
		temp->item = newItem;
		temp->next = head;
		head = temp;
	}

Could you explain why "typename" is needed? :)
Thank you.
Hi

I think as far as the LinkNode is a private member(sub-class) the code would not work.

@jwings
jwings asked
Could you explain why "typename" is needed? :)

You have to tell the compiler that LinkedList<T>::LinkNode a type name is
you should type typename because you have a qualified name, LinkedList, that refers to a type and depends on a template parameter T
Last edited on
Oh Thank you...
I'm still new to C++ programming and especially to this template thing.

but LinkNode can be a private member, because before I change to template, it can work! :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string>
using namespace std;
#include"LinkedList.h"


int main() {
	LinkedList<int> ll;
	
	//string str;
	//int i;
	//ll.addItem(i = 0, 0);
	//ll.addItem(i = 1, 1);
	//ll.addItem(i = 2, 1);
	//ll.addItem(i = 2, 1);

	//ll.deleteItem(0);
	//ll.deleteItem(2);

	//str = ll.toString();
	//cout << str << endl;
	return 0;
}


Now I'm trying to write main function to test the program. However, there are two errors :

1) Error 1 error LNK2019: unresolved external symbol "public: __thiscall LinkedList<int>::~LinkedList<int>(void)" (??1?$LinkedList@H@@QAE@XZ) referenced in function _main C:\Users\xxxxx\linkedlisttemplate\linkedlisttemplate\main.obj

2)Error 2 error LNK2019: unresolved external symbol "public: __thiscall LinkedList<int>::LinkedList<int>(void)" (??0?$LinkedList@H@@QAE@XZ) referenced in function _main C:\Users\xxxxx\Documents\COURSE\CG1103\lab_cc\linkedlisttemplate\linkedlisttemplate\main.obj


Why is there always an error? T___T


Ok I see the problem

You cant implement template class-methdos in a source file, in a *.cpp file

You have three options to do this;

1-> implement the Methods directly in the body of the class itself, so by default all your methods are inline
1
2
3
4
5
6
7
8
9
10
11
template<class T>
class Dummy{

 // do some declaration and defination
        Dummy(){
        }
     ~Dummy(){
       }

//do some other things
};

2-> implement them at the same header file, after you declared the class,
after };, like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<class T>
class Dummy{

 // do some declaration 
};

// now implement the class methods here at the same file

template<class T>
Dummy::Dummy(){
}


// do some other things




3-> implement them in another header file, for example


1
2
3
4
5
6
template<class T>
class Dummy{
 // do some declaration
};

 #include "dummy_impl.h" 


and implement your class-medthods in dummy_impl.h file

Note: the dummy_impl.h must not include dummy.h file
Last edited on
I'm using the method two and now the problem is solved.
Thank you very much...

but I wonder why I cannot implement the function in the .cpp file like when I did with other class functions with no template...

So, if i have template, i cannot implement it in another cpp file. Is my understanding correct?
Hi

@jwings
said
So, if i have template, i cannot implement it in another cpp file. Is my understanding correct?


Yes you can't implement template class and it's methods in a source file You have to use one of the above listed method to implement your template class

Last edited on
Topic archived. No new replies allowed.