这样的函数模板怎么运行不了?

ccandcicc 2004-03-15 04:54:00
源程序:
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)
请各位解答一下!!谢谢了!
...全文
60 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccandcicc 2004-03-15
  • 打赏
  • 举报
回复

!!
我换了dev通过了
谢谢各位了~~
现在结分!
angelo23 2004-03-15
  • 打赏
  • 举报
回复
FT~漏了一句话:如果你这里ia不是数组而是定义为int *ia, 那就不能通过了,因为这样size就无法确定,而且ia也不是一个数组~
angelo23 2004-03-15
  • 打赏
  • 举报
回复
1. "不过像;min(ia)这样的函数都没有指定size的值~"
在这里你的ia是一个数组,其大小已经确定了,而min的参数表里是对一个数组的引用,所以size完全可以确定的。具体怎么实现的那就是编译器的任务了,你放心用就是了:)
2. 这次#include <iostream> 和using namespace std;放错地方了:)
你前面已经用到了cout<<size<<endl;所以这两句得放在最前面,呵呵
这次的这些错误都是vc的问题。换个更支持标准的编译器即可(前面已经说了)

嗯,吃饭去了^_^
ccandcicc 2004-03-15
  • 打赏
  • 举报
回复
新的源代码:
template <typename Type,int size >
Type min(Type (&r_array)[size])
{
cout<<size<<endl;
Type min_val=r_array[0];
for(int i=1;i<min_val;++i)
if(r_array[i]<min_val)
min_val=r_array[i];
return min_val;
}

int ia[]={10,7,14,3,25};
double da[]={10.2,7.1,14.5,3.2,25.0,16.8};
#include <iostream>
using namespace std;
int main()
{
int i=min(ia);
if(i!=3)
cout<<"??oops :integer min() failed\n";
else cout<<"!!ok :integer min() worked\n";
double d=min(da);
if(d!=3.2)
cout<<"??oops :double min() failed \n";
else cout <<"!!ok :double min() worked \n";
return 0;
}
编译结果:
--------------------Configuration: Àà - Win32 Debug--------------------
Compiling...
defineClass.cpp
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(2) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
C:\Documents and Settings\chenyun1\×ÀÃæ\program\Àà\defineClass.cpp(18) : 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(22) : error C2784: 'Type __cdecl min(Type (&)[1])' : could not deduce template argument for ' (&)[1]' from 'double [6]'
Error executing cl.exe.

Àà.exe - 3 error(s), 0 warning(s)
ccandcicc 2004-03-15
  • 打赏
  • 举报
回复
我觉得这样的函数和书中所说的程序执行原理有点矛盾
书中说:只有当模板函数的参数被唯一确定的时候函数才可以执行
不过像;min(ia)这样的函数都没有指定size的值~
也就是说没有一个机制可以让size的值确定下来~

这究竟是怎样的一个问题呢???
ccandcicc 2004-03-15
  • 打赏
  • 举报
回复
to:angelo23(angelo)
#include 和int这个不是这个程序地关键原因
上面我想知道地是;原型:min(Tpye (&r_array)[size])函数在实例化:min(ia)地时候能不能指定size地大小

我地源代码是从primer那里抄下来地~~里面地解释是当min(ia)调用时候可以指定size地大小~
zouxueping 2004-03-15
  • 打赏
  • 举报
回复
Type min( Type (&r_array)[size])
改成Type min( Type (&r_array)[], int size)也行吧
angelo23 2004-03-15
  • 打赏
  • 举报
回复
1.你的源码没贴全,include就没有:)
2.for (itn i=1;i<size;++i)这里int拼错了
3.最关键的:vc++6.0不支持这样的模板非类型参数。也就是说代码本身并没有问题(这不是C++ Primer上用来讲解模板非类型参数的吗,呵呵),是编译器的问题。用vc++7.1或者g++3.2及以上版本(其它的编译器没试过,不敢说,呵呵)
gaowenjun233 2004-03-15
  • 打赏
  • 举报
回复
Type min( Type (&r_array)[size])
数组的大小好像不能间接传递给函数
应该给size指定一个明确的值.
这样好像不行,即使行也不好,
用迭代器传递更好
for (itn i=1;i<size;++i)
int
加using namespace std;

64,266

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧