I have a below code.
#include <iostream>
template <class T,class U>
T myMax(T x, T y)
{
return (x > y)? x: y;
}
int main()
{
std::cout << myMax(3, 7) << std::endl; // Call myMax for int
std::cout << myMax(3.0, 7.0) << std::endl; // call myMax for double
std::cout << myMax('g', 'e') << std::endl; // call myMax for char
return 0;
}
On compiling the code, compiler reports an error as show below.
functionTemplates.cpp: In function ‘int main()’: functionTemplates.cpp:18: error: no matching function for call to ‘myMax(int, int)’ functionTemplates.cpp:19: error: no matching function for call to ‘myMax(double, double)’ functionTemplates.cpp:20: error: no matching function for call to ‘myMax(char, char)’
I know that if i remove class U, compilation will be successful. But i want to know why does compiler bothers about an unused parameter?
Aucun commentaire:
Enregistrer un commentaire