Template based Linked list

Hello,

I am writing a template based linked list for a class and am running into some issues. I dont know why but just including my LinkedListT.h file in my driver file gives me a bunch of errors unrelated to the linked list, that weren't there before i included it. I have tried to search and find some help but cant seem to come up with anything

LinkedListT.h:
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
#include <iostream>

using namespace std;

template <typename T>
class LinkedList {
	public:
		LinkedList();
		LinkedList(const LinkedList& aLinkedList);
		~LinkedList();
		bool isEmpty() const;
		bool get(int index, T& itemAtIndex) const;
		void add(const T& newItem);
		void remove(int index);
		void print();
	private:
		struct node {
			T item;
			node *next;
		};
		node *topPtr;
		node *tail;
};

#include "LinkedListT.cpp" 


LinkedListT.cpp:
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
100
101
102
103
104
105
106
107
#ifdef LINKEDLISTT_H
#include <new>
#include <iostream>

template <typename T>
LinkedList<T>::LinkedList() : topPtr(NULL) { }

template <typename T>
LinkedList<T>::LinkedList(const LinkedList& aLinkedList) {
	if (aLinkedList.topPtr == NULL)
		topPtr == NULL;
	else {
		topPtr = new node;
		topPtr->item = aLinkedList.topPtr->item;

		node *newPtr = topPtr;
		for (node *origPtr = aLinkedList.topPtr->next;
		     origPtr != NULL; origPtr = origPtr->next) {
			newPtr->next = new node;
			newPtr = newPtr->next;
			newPtr->item = origPtr->item;
			tail = origPtr;
		}
		newPtr->next = NULL;
	}
}

template <typename T>
LinkedList<T>::~LinkedList() {
	while (!isEmpty())
		remove();
}

template <typename T>
bool LinkedList<T>::isEmpty() const {
	return (topPtr == NULL);
}

template <typename T>
bool LinkedList<T>::get(int index, T& itemAtIndex) const {
	if (index < 1)
		return false;
	else {
		node *cur = topPtr;
		for(int skip = 1; skip < index; ++skip)
			cur = cur->next;
		if(cur != NULL) {
			itemAtIndex = cur->item;
			return true;
		}
		else 
			return false;
	}
}

template <typename T>
void LinkedList<T>::add(const T& newItem) {
	node *newNode = new node;
	
	newNode->item = newItem;
	newNode->next = NULL;

	if(isEmpty())
		topPtr = newNode;
	else
		tail->next = newNode;
	
	tail = newNode;
}

template <typename T>
void LinkedList<T>::remove(int index) {
	node *cur;

	if(index == 1) {
		cur = topPtr;
		topPtr = topPtr->next;
	}
	else {
		node *prev;
		node temp;
		if(get(index-1, temp))
			prev = &temp;
		else
			return;
		cur = prev->next;
		if(cur == tail)
			tail = prev;
		prev->next = cur->next;
	}
	
	cur->next = NULL;
	delete cur;
	cur = NULL;
}

template <typename T>
void LinkedList<T>::print() {
	node *cur;

	while(cur != NULL) {
		cout << (string)cur->item << endl;
		cur = cur->next;
	}
}

#endif 


and the driver:
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
#include <iostream>
#include "Entry.h"
#include "LinkedListT.h"

using namespace std;

int main()
{
    Key k1;
    k1.setWord("Key class test 1/3");
    cout << k1.getWord() << "\n";
    Key k2("Key class test 2/3");
    cout << k2.getWord() << "\n";
    k1.setWord("Key class test 3/3");
    Key k3(k1);
    cout << k3.getWord() << "\n";
    cout << boolalpha << (k1 == k3) << "\n";
    cout << boolalpha << (k1 < k2) << "\n";
    cout << boolalpha << (k1 > k2) << "\n";
    cout << "What is a key? ";
    cin >> k1;
    cout << k1;

    Entry e1;
    e1.setDefinition("Entry class test 1/3");
    cout << e1.getDefinition() << "\n";
    Entry e2("Entry class test 2/3");
    cout << e2.getDefinition() << "\n";
    e1.setDefinition("Entry class test 3/3");
    Entry e3(e1);
    cout << e3.getDefinition() << "\n";
    cout << boolalpha << (e1 == e3) << "\n";
    cout << boolalpha << (e1 < e2) << "\n";
    cout << boolalpha << (e1 > e2) << "\n";
    cout << e3;
    cout << "What is a Definition? ";
    cin >> e1;
    cout << e1;

    return 0;
}


like i said before i add the line
#include "LinkedListT.h"

to the driver it runs fine and properly. I am assuming it is some sort of syntax thing or something. Thank you!
these are the errors:
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

/tmp/ccgYyOmJ.o: In function `main':
test.cpp:(.text+0x8e): undefined reference to `Key::Key()'
test.cpp:(.text+0xc5): undefined reference to `Key::setWord(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
test.cpp:(.text+0x10e): undefined reference to `Key::getWord() const'
test.cpp:(.text+0x1be): undefined reference to `Key::Key(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
test.cpp:(.text+0x207): undefined reference to `Key::getWord() const'
test.cpp:(.text+0x2b7): undefined reference to `Key::setWord(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
test.cpp:(.text+0x300): undefined reference to `Key::Key(Key const&)'
test.cpp:(.text+0x336): undefined reference to `Key::getWord() const'
test.cpp:(.text+0x39d): undefined reference to `Key::operator==(Key const&) const'
test.cpp:(.text+0x3ee): undefined reference to `Key::operator<(Key const&) const'
test.cpp:(.text+0x43f): undefined reference to `Key::operator>(Key const&) const'
test.cpp:(.text+0x4a5): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, Key&)'
test.cpp:(.text+0x4b8): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Key const&)'
/tmp/ccMbALPT.o: In function `operator>>(std::basic_istream<char, std::char_traits<char> >&, Entry&)':
Entry.cpp:(.text+0xfc): undefined reference to `Key::setWord(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccMbALPT.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Entry const&)':
Entry.cpp:(.text+0x3b9): undefined reference to `Key::getWord() const'
/tmp/ccMbALPT.o: In function `Entry::Entry(Entry const&)':
Entry.cpp:(.text+0x494): undefined reference to `Key::Key()'
/tmp/ccMbALPT.o: In function `Entry::Entry(Entry const&)':
Entry.cpp:(.text+0x53e): undefined reference to `Key::Key()'
/tmp/ccMbALPT.o: In function `Entry::Entry(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Entry.cpp:(.text+0x5e8): undefined reference to `Key::Key()'
/tmp/ccMbALPT.o: In function `Entry::Entry(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Entry.cpp:(.text+0x65e): undefined reference to `Key::Key()'
/tmp/ccMbALPT.o: In function `Entry::Entry()':
Entry.cpp:(.text+0x6d4): undefined reference to `Key::Key()'
/tmp/ccMbALPT.o:Entry.cpp:(.text+0x71c): more undefined references to `Key::Key()' follow
collect2: ld returned 1 exit status 
closed account (DSLq5Di1)
The header guard should be in your header, and the implementation (cpp file) includes the header, not the other way around.

LinkedListT.h:
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef LINKEDLISTT_H
#define LINKEDLISTT_H

#include <new>
#include <iostream>

template <typename T>
class LinkedList {
	...
};

#endif 


LinkedListT.cpp:
1
2
3
4
5
6
#include "LinkedListT.h"

template <typename T>
LinkedList<T>::LinkedList() : topPtr(NULL) { }

...

As for the errors, where is 'Key' defined? include the appropriate header for 'Key' in "the driver" and "Entry.h".
Topic archived. No new replies allowed.