vc下一个模板程序的困惑

westdo westdo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
同一段代码,在gcc中编译通过,并且能正常执行,但是到了vc下报错.请各位弟兄帮忙.代码如下

#include <iostream>
using namespace std;

template <class Type,int size> Type min(Type (&r_array)[size])
{
Type min_val=r_array[0];
for(int i=1;i<size;++i)
{
if(r_array[i]<min_val)
min_val=r_array[i];
}
return min_val;
}
int ia[]={10,6,7,3,23};
double da[]={10.2,7.1,14.5,3.2,25.0,16.8};

int main()
{


int i=min(ia);
if(i!=3)
cout<<"failed!"<<endl;
else
cout<<"ok!"<<endl;

double d=min(da);
if(d!=3.2)
cout<<"failed!"<<endl;
else
cout<<"ok!"<<endl;

return 0;
}
编译的时候报错
min.cpp
E:\ctest\min.cpp(4) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
E:\ctest\min.cpp(34) : error C2784: 'Type __cdecl min(Type (&)[1])' : could not deduce template argument for ' (&)[1]' from 'int [5]'
E:\ctest\min.cpp(40) : error C2784: 'Type __cdecl min(Type (&)[1])' : could not deduce template argument for ' (&)[1]' from 'double [6]'
Error executing cl.exe.
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
westdo
C++ 语言 65,214 成员 250,514 内容