c++ 显示具体化的问题
请教下各位,程序如下(关于模板的现实具体化)
#include <iostream>
using namespace std;
template <class T>
T max5(T a[], int );
template <int> int max5(int , int);
template < > double max5< double>(double da[],double);
int main()
{
int ia[5] = {23,89,11,39,4};
double da[5] = {23.2,89.1,11.6,39.7,4.9};
cout << "test 5 int: 23 89 11 39 4" << endl;
max5(ia,5);
cout << endl << "test 5 int: 23.2 89.1 11.6 39.7 4.9" << endl;
max5(da,5.0);
system("pause");
return 0;
}
template <class T>
T max5(T a[], int no)
{
int i = 0;
for(i = 0; i < no - 1; i++)
{
if(a[i] > a[i + 1])a[i + 1] = a[i];
}
cout << "maximus is : " << a[i];
}
template < >
double max5(double da[],double)
{
cout << "test double" << endl;
return 0.0;
}
为什么程序会报错: template-id `max5<double>' for `double max5(double*, double)' does not match any template declaration (红色字体处)
不明白其中到位为何?麻烦各位明白人给点提示!
[解决办法]
template <class T>
T max5(T a[], int no)
{
int i = 0;
for(i = 0; i < no - 1; i++)
{
if(a[i] > a[i + 1])a[i + 1] = a[i];
}
cout << "maximus is : " << a[i];
}
template <> int max5<int>(int a[], int)
{
cout<<"test"<<endl;
return 0;
}
template <> double max5<double>(double da[],int)
{
cout<<"Test"<<endl;
return 0;
}
int main()
{
int ia[5] = {23,89,11,39,4};
double da[5] = {23.2,89.1,11.6,39.7,4.9};
cout << "test 5 int: 23 89 11 39 4" << endl;
max5(ia,5);
cout << endl << "test 5 int: 23.2 89.1 11.6 39.7 4.9" << endl;
max5(da,5);
system("pause");
return 0;
}