Write template function that returns the maximum of 3 values.
Take into account that according to the assignment the function has to return the maximum. It shall not do something else as for example outputing on the console some values.
1 2 3 4 5 6 7 8
template <class T>
T findMax( T n1, T n2, T n3 )
{
T max = n1 < n2 ? n2 : n1;
max = max < n3 ? n3 : max;
return ( max );
}
EDIT: By the way it would be much better to define function parameters as const references because they are not changed in the function. So the function can be written as follows
1 2 3 4 5 6 7 8
template <class T>
T findMax( const T &n1, const T &n2, const T &n3 )
{
T max = n1 < n2 ? n2 : n1;
max = max < n3 ? n3 : max;
return ( max );
}
Well i got it to work, but i had to cout<<max in the function, i tried to add a line in the main program after i called the function
cout<<max<<endl;
but it said there was something wrong with the "<<"
any ideas