function template

I've been working on this problem for a while and this error is trowing me off. Maybe someone can help point out what it means. thanks

(14): error C2783: 'void maximum(int,int,int,int)' : could not deduce template argument for 'T'

maximum.h(4) : see declaration of 'maximum'




//maximum.h

template < typename T>
void maximum( int value1, int value2, int value3, int value4 )
{
int maximumValue = value1;

if ( value2 > maximumValue )
maximumValue = value2;

if ( value3 > maximumValue )
maximumValue = value3;

if ( value4 > maximumValue )
maximumValue = value4;

return maximumValue;
}



//main function

#include <iostream>
#include "maximum.h"
using namespace std;

int main ()
{

int num1, num2, num3, num4;

cout<< "Input 4 integer values: " <<endl;
cin>> num1 >> num2 >> num3 >> num4;

cout<< "The largest value is: "
<< maximum( num1, num2, num3, num4 ); /*no instance of function template "maximum" matches the arguement list*/

}
Alright. The way templates work is if you don't implicitly tell them what arguments to accept (the <typename T> in your template), then the program tries to deduce it for itself.

In this case, it doesn't matter which type you call, the function does exactly the same thing. So it has no idea what typename to have.

The whole template<typename T> thing is completely unnecessary in this code, I see no reason to include it.
Last edited on
Why is this function a template, you never even use the template parameter?

But basically, the problem is that the compiler can't determine the template parameter from your function call so you have to specify it.
so should I make the typename int instead of T?
Im using C++ how to program eight edition by Deitel and it doesnt explain this topic very well.
No, you should be replace all of the "int" in the function definition with T. Go look at the template part of the this tutorial on this site.
where can I find the tutorials on this site?
Topic archived. No new replies allowed.