在下面的程序,爲什麼編譯器生成的調用printMax
模板函數,而不是調用printMaxInts
功能的錯誤?爲什麼編譯器不能將此調用解析爲模板函數?
#include <iostream>
template<class A>
void printMax(A a,A b)
{
A c = a>b?a:b;
std::cout<<c;
}
void printMaxInts(int a ,int b)
{
int c = a>b?a:b;
std::cout<<c;
}
int main()
{
printMax(1,14.45);
printMaxInts(1,24);
}
另一個解決方法是取兩個模板參數,在函數中執行轉換。 – Potatoswatter 2010-08-28 19:00:01