I'm working through an algorithm text, trying to implement everything in C++ for practice. But I can't seem to figure out templating.I have three files: algPlayground.h
#include <stdlib.h>
#include <vector>
using namespace std;
template <class T> void insertionSort(vector<T>& toSort);
algPlayground.cpp
#include <stdlib.h>
#include <vector>
#include "algPlayground.h"
using namespace std;
template <class T> void insertionSort(vector<T>& toSort) {
for (int j=1; j < toSort.size(); ++j) {
T key = toSort[j];
int i = j-1;
while (i > -1 && toSort[i] > key) {
toSort[i+1] = toSort[i];
i -= 1;
} // end while
toSort[i+1] = key;
} // end for
} // end insertionSort
and algTest.cpp
#include <stdlib.h>
#include <vector>
#include <iostream>
#include "algPlayground.h"
using namespace std;
int main() {
vector<int> vectorPrime(5);
vectorPrime[0]=5;
vectorPrime[1]=3;
vectorPrime[2]=17;
vectorPrime[3]=8;
vectorPrime[4]=-3;
insertionSort(vectorPrime);
for (int i=0; i<vectorPrime.size(); ++i) {
cout << vectorPrime[i] << " ";
}// end for
}
I get the following error:
algTest.cpp:(.text+0xb1): undefined reference to `void insertionSort<int>(std::vector<int, std::allocator<int> >&)'
collect2: error: ld returned 1 exit status
I saw this thread where, someone suggested that the proper way to do this was
template<typename T, typename A>
void some_func( std::vector<T,A> const& vec ) {
}
but when I make that correction, I'm still getting a similar error:
algTest.cpp:(.text+0xb1): undefined reference to `void insertionSort<int, std::allocator<int> >(std::vector<int, std::allocator<int> >&)'
collect2: error: ld returned 1 exit status
I have no idea where I'm going wrong here. Help?