Binary search tree error LNK2019

I'm making a template binary search tree class and I'm having the following error:

error LNK2019: unresolved external symbol "public: void __thiscall Tree<int>::insert(int)" (?insert@?$Tree@H@@QAEXH@Z) referenced in function _main

for three different functions (insert, default constructor, destructor)
Any idea what I'm doing wrong? It doesn't seem to be an issue with VS.


Header:
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#pragma once
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

template <class Item>
struct Node
{
	//Default constructor for Node
	//Purpose: Initialize all values
	//Parameters: None
	//Returns: None
	Node();

	//Parameterized constructor for Node
	//Purpose: Initialize all values
	//Parameters: a data Item, and Node pointers for left and right children
	//Returns: None
	Node(Item, Node<Item>*, Node<Item>*);

	//variables
	Item data;
	Node<Item> *lchild;
	Node<Item> *rchild;
};

template <class Item>
class Tree
{

public:
	//Default constructor for Tree
	//Purpose: Initialize all values
	//Parameters: None
	//Returns: None
	Tree();



	//insert
	//Purpose: To add a value to tree
	//Parameters: A const reference to an Item
	//Returns: None
	void insert(Item);

	//copy
	//Purpose: To copy values
	//Parameters: Node pointer
	//Returns: None
	void copy(Node<Item>*);

	//chop
	//Purpose: Delete tree
	//Parameters: Node pointer
	//Returns: None
	void chop(Node<Item>*);

	//Copy constructor
	//Purpose: create new Tree
	//Parameters: A Tree object
	//Returns: Tree
	Tree(const Tree&);

	//Destructor
	//Purpose: Clean up data, delete tree
	//Parameters: None
	//Returns: None
	~Tree();

	//operator=
	//Purpose: Overload assignment operator
	//Parameters: a const ref to a Tree object
	//Returns: Tree
	const Tree<Item>&operator=(const Tree&);

	//inOrderTraverse
	//Purpose: To traverse the tree in order
	//Parameters: A Node pointer
	//Returns: None
	//void inOrderTraverse(Item);


private:
	Node<Item> *root;
};




.cpp:


#include "Tree.h"

template <class Item>
Node<Item>::Node()
{
	data = 0;
	lchild = nullptr;
	rchild = nullptr;
}

template <class Item>
Node<Item>::Node(Item _data, Node<Item> *_lchild, Node<Item> *_rchild)
{
	data = _data;
	lchild = _lchild;
	rchild = _rchild;
}

template <class Item>
Tree<Item>::Tree()
{
	root = nullptr;
}


template <class Item>
void Tree<Item>::insert(Item item)
{
	Node<Item> *new_node = new Node<Item>();
	new_node->data = item;

	if (root == nullptr)
	{
		root = new_node;
	}
	else
	{
		Node<Item> *p = root;
		Node<Item> *q = nullptr;
		while (p->data != item)
		{
			q = p;
			if (item < p->data)
			{
				p = p->lchild;
			}
			else if (item > p->data)
			{
				p = p->rchild;
			}
		}
		if (p->data == item)
		{
			cout << "Duplicate Data" << endl;
		}
		else
		{
			if (item < q->data)
			{
				q->lchild = new_node;
			}
			else
			{
				q->rchild = new_node;
			}
		}

	}
}


template <class Item>
void Tree<Item>::copy(Node<Item> *c)
{
	if (c)
	{
		insert(c->data);
		copy(c->lchild);
		copy(c->rchild);
	}
}

template <class Item>
void Tree<Item>::chop(Node<Item> *c)
{
	if (c)
	{
		chop(c->lchild);
		chop(c->rchild);
		delete c;
	}
}

template <class Item>
Tree<Item>::Tree(const Tree& t)
{
	root = nullptr;
	copy(t.root);
}

template <class Item>
Tree<Item>::~Tree()
{
	chop(root);
}

template <class Item>
const Tree<Item>&Tree<Item>::operator=(const Tree& t)
{
	if (this != &t)
	{
		chop(root);
		root = nullptr;
		copy(t.root);
	}
	return *this;
}





Driver:


#include "Tree.h"

int main()
{
	//variables for data
	string file;
	ifstream infile(file);
	Tree<int> myTree;
	int d = 0;

	//ask user to enter filename
	cout << "Please enter the file you wish to open: " << endl;
	getline(cin, file);
	infile.open(file.c_str());

	//make sure filename is valid
	while (!infile)
	{
		cout << "Error opening file, please enter the name of the file you wish to open: " << endl;
		getline(cin, file);
		infile.open(file.c_str());
	}

	//if file is good, build tree
	while (!infile.eof())
	{
		myTree.insert(d);
		infile >> d;
		cout << d << endl;
	}

	//cout << "Traverse" << endl;
	//inOrderTraverse(d);

	system("PAUSE");
	return 0;
}


Last edited on
Topic archived. No new replies allowed.