这样的函数模板怎么运行不了?
源程序:
template <typename Type ,int size>
Type min( Type (&r_array)[size])
{
Type min_val=r_array[0];
for (itn i=1;i<size;++i)
if(r_array[i]<min_val)
min_val=r_array[i];
return min_val;
}
int ia[]={10,7,14,3,25};
double da[6]={10.2,7.1,14.5,3.2,25.0,16.8};
#include <iostream>
int main()
{
int i=min(ia);
if(i!=3)
cout<<"??oops :integer min() failed\n";
else cout<<"!!ok: integer min() woeked\n";
double d=min(da);
if(d!=3.2)
cout<<"??oops :double min() fialed\n";
else cout<<"!!ok :double min() worked\n";
return 0;
}
编译结果:
--------------------Configuration: Àà - Win32 Debug--------------------
Compiling...
defineClass.cpp
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(3) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(16) : error C2784: 'Type __cdecl min(Type (&)[1])' : could not deduce template argument for ' (&)[1]' from 'int [5]'
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(18) : error C2065: 'cout' : undeclared identifier
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(18) : error C2297: '<<' : illegal, right operand has type 'char [30]'
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(19) : error C2297: '<<' : illegal, right operand has type 'char [28]'
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(20) : error C2784: 'Type __cdecl min(Type (&)[1])' : could not deduce template argument for ' (&)[1]' from 'double [6]'
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(22) : error C2297: '<<' : illegal, right operand has type 'char [29]'
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(23) : error C2297: '<<' : illegal, right operand has type 'char [27]'
Error executing cl.exe.
Àà.exe - 8 error(s), 0 warning(s)
请各位解答一下!!谢谢了!