(SOLVED) template heap data structure giving linker error 2019

Mar 30, 2009 at 12:27am
I'm working on a class project, using Vista 64bit, MSVC 2008 Express. The heap sort that I'm trying to use will not build because of my implementation of the heap data structure. I need to use removemax() and siftdown() when heapsort is called. Here is the code:

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
// implementation of max-heap class

#include "maxheap.h"
#include "help.h"

template<class Elem>	// Utility function
void maxheap<Elem>::siftdown(int pos) {
	while (!isLeaf(pos)) {	// Stop if pos is a leaf
		int j = leftchild(pos);
		int rc = rightchild(pos);
		if ((rc < n) && (Heap[j] < Heap[rc]))
			j = rc;	// Set j to greater child's value
		if (!(Heap[pos] < Heap[j]))
			return;		 // Done 
		swap(Heap, pos, j);
		pos = j;	// Move down
	}
}

template<class Elem>	// Insert element
bool maxheap<Elem>::insert(const Elem& val) {
	if (n >= size) return false;	// Heap is full
	int curr = n++;
	Heap[curr] = val; // Start at end of heap
	// Now sift up until curr's parent > curr
	while ((curr != 0) && ((Heap[curr] > Heap[parent(curr)]))) {
		swap(Head, curr, parent(curr));
		curr = parent(curr);
	}
	return true;
}

template<class Elem>	// Remove max value
bool maxheap<Elem>::removemax(Elem& it) {
	if (n == 0)
		return false;	// Heap is empty
	swap(Heap, 0, --n);	// Swap max with last value
	if (n != 0)
		siftdown(0);	 // Siftdown new root val
	it = Heap[n];		// Return deleted value
	return true;
}

// Remove value at specified position
template<class Elem>
bool maxheap<Elem>::remove(int pos, Elem& it) {
	if ((pos < 0) || (pos >= n)) 
		return false;	// bad position
	swap(Heap, pos, --n);	// swap with last value
	while ((pos != 0) && ((Heap[pos] > Heap[parent(pos)])))
		swap(Heap, pos, parent(pos));  // Push up if large key
	siftdown(pos);	// Push down if small key
	it = Heap[n];
	return true;
}



Here is the error message as well:

1>------ Build started: Project: sorts, Configuration: Debug Win32 ------
1>Compiling...
1>maxheap.cpp
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "public: bool __thiscall
maxheap<int>::removemax(int &)" (?removemax@?$maxheap@H@@QAE_NAAH@Z) referenced
in function "void __cdecl heapsort<int>(int * const,int)" (??$heapsort@H@@YAXQAHH@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall
maxheap<int>::siftdown(int)" (?siftdown@?$maxheap@H@@QAEXH@Z) referenced
in function "public: void __thiscall maxheap<int>::buildHeap(void)" (?buildHeap@?$maxheap@H@@QAEXXZ)
1>Z:\Documents\Programmed Programs\C++\sorts\Debug\sorts.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://z:\Documents\Programmed Programs\C++\sorts\sorts\Debug\BuildLog.htm"
1>sorts - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any suggestions or advice would be very helpful.
Last edited on Mar 30, 2009 at 5:34am
Mar 30, 2009 at 5:31am
Solved my problem, I forgot to enter in the code for the right child in the header file.
Topic archived. No new replies allowed.