No. It should look like this: template <class T> double Driver::average(const vector<T>& num)
(I did say before double.)
[quote]And i have declared template <class T> double average(const std::vector<T>&); in the Driver.cpp as well as per your advice. Thanks.[/code]
You don't understand. The declaration must remain in the header. Otherwise, how are other files goign to know what the function they're calling is supposed to look like? What needs to be moved is the defintion. For some reason I have yet to grasp, template functions must be declared and defined in the same file.
For example, if you do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//header.h
template <typename T> T sum(T a,T b);
//header.cpp
#include "header.h"
template <typename T> T sum(T a,T b){
return a+b;
}
//main.cpp
#include "header.h"
int main(){
int a=10,b=20,c=sum(a,b);
return 0;
}
That code will not compile, because the compiler can't find template function definitions in separate files. One way to get around this is including the .cpp, but that gives problems if the file contains other non-template function definitions. What you need to do, is declare and define the function at the same time: