I have a simple template function,
A.h
#include <iostream>
#include <vector>
using namespace std;
template<class T>
pair<T, T> GetMatchedBin(T, vector<pair<T, T> >);
A.cpp
#include "A.h"
template<class T>
pair<T, T> GetMatchedBin(T val, vector<pair<T, T> > &bins)
{
for(unsigned int i=0; i<bins.size(); i++){
if(val >= bins[i].first && val < bins[i].second)
return bins[i];
}
return pair<T, T>();
}
which I call via,
main.cpp
#include <iostream>
#include <vector>
#include "A.h"
using namespace std;
int main()
{
vector<pair<int, int> > bins;
bins.push_back(pair<int, int>(0, 1));
bins.push_back(pair<int, int>(1, 2));
bins.push_back(pair<int, int>(2, 3));
bins.push_back(pair<int, int>(3, 4));
pair<int, int> matched_bin = GetMatchedBin(3, bins);
cout << matched_bin.first << ", " << matched_bin.second << endl;
return 0;
}
However when I try to compile this I get the error,
make
g++ -o temp temp.cpp A.o
/tmp/dvoong/ccoSJ4eF.o: In function `main':
temp.cpp:(.text+0x12a): undefined reference to `std::pair<int, int> GetMatchedBin<int>(int, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >)'
collect2: ld returned 1 exit status
make: *** [temp] Error 1
The odd thing is that if I do this all in one file rather than dividing into header and .cpp files then it works...
Any idea what is causing this?
Thanks