Solved
Overloading a function template
Posted on 2006-04-20
Here is a function template that will accept two arguments. I must then write the function to accept three arguments and calculate the average of these arguments. I have just included my template and function- no errors anywhere else.
template <class T>
T average(T a1, T b2);
T average(T a1,T b2, T c3); //Overloaded function
template<class T>
T average(T a1, T b2);
T average(T a1, T b2, T c3);
{ if (number = 2)
average = (a1 + b2) / 2;
else if (number = 3)
average = (a1 +b2 +c3 ) / 3;
else
cout << "Try agian" <<endl;
return double average;
}
Here are the errors I am getting that is related to this code.
error C2146: syntax error : missing ';' before identifier 'average'
error C2501: 'T' : missing storage-class or type specifiers
error C2146: syntax error : missing ')' before identifier 'a1'
error C2501: 'average' : missing storage-class or type specifiers
error C2365: 'average' : redefinition; previous definition was a 'function'
error C2059: syntax error : ')'
I don't understand what is wrong with my template, it is basically written straight from the book. Please help.