According to this:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
One way to keep only the function declaration in the .h file is to do this
////////// file: Tools.h
#include <iostream>
#include <vector>
using namespace std;
template <typename T> T Sum(vector<T> &V);
///////// file: Tools.cpp
#include "Tools.h"
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
T Sum(vector<T> &V)
{
T sum = static_cast<T>(0.0);
for(unsigned int i = 0; i < V.size(); i++)
sum += V[i];
return sum;
}
//this is the key line to tell the compiler to actually make this function for unsigned int
template unsigned int Sum<unsigned int>(vector<unsigned int> &V);
However, what if you want this function for a type that Tools does not know about? ie
template Point Sum<Point>(vector<Point> &V);
will not work because Point has not been defined. If you make Tools depend on Point, but Point already depends on Tools, then there is a circular problem.
Any suggestions?
Thanks,
Dave