My Project is to create a MaxHeap priority queue to sort voters. Everything compiles fine but the linker gives me a wierd error:

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall MaxHeap<class Voter,class SalaryCompare>::MaxHeap<class Voter,class SalaryCompare>(class Voter * * const,int,int)" (??0?$MaxHeap@VVoter@@VSalaryCompare@@@@QAE@QAPAVVoter@@HH@Z) referenced in function _main

Anyone have any ideas?

The MaxHeap.h is:

#pragma once

template<class Elem, class Comp>
class MaxHeap {
public:
	MaxHeap(Elem *[], int, int);
	bool insert(Elem * element);
	bool removeMax(Elem *&element);
	bool remove(int pos, Elem *&element);
	void buildHeap();
	int heapsize();
	bool isLeaf(int pos);
	int leftChild(int pos);
	int rightChild(int pos);
	int parent(int pos);

private:
	Elem **Heap;
	int size;
	int n;
	void siftdown(int);
	void swap(int, int);
};

and the Constructor I'm calling is:

template<class Elem, class Comp>
MaxHeap<Elem, Comp>::MaxHeap(Elem *h[], int num, int max) {
	Heap = new Elem *[max];
	n = num;
	size = max;
	for(int i = 0; i < num; i++)
		Heap[i] = h[i];
	buildHeap();
}

And finally the main function:

const int MAX_SIZE = 5000;
int main() {
	Voter *voterArr[MAX_SIZE];
	int numRec =  makeList(voterArr);
	MaxHeap<Voter, SalaryCompare>  voters(voterArr, numRec, MAX_SIZE);
	return 1;
}

note:I'm using Visual Studio 2008 Pro

> template<class Elem, class Comp>
> MaxHeap<Elem, Comp>::MaxHeap(Elem *h[], int num, int max) {
Is this also in your header file?

It needs to be, otherwise the compiler can't create the instance of the template which deals with <Voter, SalaryCompare> and then the linker complains about "no such function".

> template<class Elem, class Comp>
> MaxHeap<Elem, Comp>::MaxHeap(Elem *h[], int num, int max) {
Is this also in your header file?

Does this not count?

template<class Elem, class Comp>
class MaxHeap {
public:
	MaxHeap(Elem *[], int, int);

That only tells the compiler it exists somewhere.

Then the linker tells you otherwise that it doesn't exist.

It's like walking into a library, finding the book you want in the card index (the prototype), then going to the self where it's supposed to be and finding NO book (the implementation).

I got it now, just forgot:

template class MaxHeap<Voter, SalaryCompare>;

at the end of my MaxHeap.cpp. Its been to long since i used template classes.

This article has been dead for over six months. Start a new discussion instead.