Possible Duplicate:
C++ template, linking error
I am attempting to implement a selection sort, but I keep getting the error(printed below). It seems to me that all my includes and templates are done correctly. Can someone explain to me the reason for this error and the general approach to debugging this type of error. It usually seems to happen when there is an include or template issues, but occasionally it occurs in situation where I have no idea what is wrong. Thanks.
error LNK2019: unresolved external symbol "public: void __thiscall Selection::SelectionSort(int * const,int)" (?SelectionSort@?$Selection@H@@QAEXQAHH@Z) referenced in function _main
test.cpp
#include <iostream>
#include "SelectionSort.h"
using namespace std;
void main()
{
int ar[] = {1,2,3,4,5};
Selection<int> s;
s.SelectionSort(ar,5);
for(int i = 0; i < 5; i++)
{
cout << "\nstudent number " << i + 1<< " grade " << ar[i];
}
}
SelcectionSort.h
template<class ItemType>
class Selection
{
public:
void SelectionSort(ItemType[], int);
private:
int MinIndex(ItemType[], int, int);
void Swap(ItemType& , ItemType&);
};
SelectionSort.cpp
#include "SelectionSort.h"
template<class ItemType>
void Selection<ItemType>::SelectionSort(ItemType values[], int numValues)
// Post: The elements in the array values are sorted by key.
{
int endIndex = numValues-1;
for (int current = 0; current < endIndex; current++)
Swap(values[current],
values[MinIndex(values, current, endIndex)]);
}
template<class ItemType>
int Selection<ItemType>::MinIndex(ItemType values[], int startIndex, int endIndex)
// Post: Returns the index of the smallest value in
// values[startIndex]..values[endIndex].
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}
template<class ItemType>
inline void Selection<ItemType>::Swap(ItemType& item1, ItemType& item2)
// Post: Contents of item1 and item2 have been swapped.
{
ItemType tempItem;
tempItem = item1;
item1 = item2;
item2 = tempItem;
}