请问哪里出错了?

taurusbbstech 2008-03-28 10:55:33
#include <iostream>
template <typename T>
void ShowArray (T arr[], int n);

template <typename T>
void ShowArray(T * arr[], int n);

struct debts
{
char name[50];
double amount;
};

int main(void)
{
using namespace std;
int things[6] = {13,31,103,301,310,130};
struct debts mr_E[3] =
{
{"Ima Wolfe", 2400},
{"Ura Foxe ", 1300},
{"Iby Stout", 1800}
};
double * pd[3];
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Listing Mr.E's counts of things: \n";
ShowArray(things, 6);
cout << "Listing Mr.E's debts:\n";
ShowArray(pd, 3);
return 0;
}

template <typename T>
void ShowArray(T arr[], int n)
{
using namespace std;
cout << "template A\n";
for (int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << endl;
}

template <typename T>
void ShowArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
for (int i = 0; i < n; i++)
cout << *arr[i] << ' ';
cout endl;
}
...全文
146 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
taurusbbstech 2008-03-28
  • 打赏
  • 举报
回复
我是用vc6,看来得换个编译器?
txwcan999 2008-03-28
  • 打赏
  • 举报
回复
4楼的 : //pd没有初始化,就直接使用
用g++ (linux下) 可以通过, 没警告

不知道用pc-lint 可以检查出警告吗?
hanlin1985 2008-03-28
  • 打赏
  • 举报
回复
刚才没注意看,LZ已经在各个函数里面加了using声明,那么就只有这个了cout<<endl
foxfactor 2008-03-28
  • 打赏
  • 举报
回复
我的DEV-CPP 可以运行呀!
hanlin1985 2008-03-28
  • 打赏
  • 举报
回复
开头缺少using声明,
using namespace std;
还有就是:
cout<<endl;
sheenl 2008-03-28
  • 打赏
  • 举报
回复
vs2005和mingw
taurusbbstech 2008-03-28
  • 打赏
  • 举报
回复
请问你用的是什么编译器?谢谢!
sheenl 2008-03-28
  • 打赏
  • 举报
回复
编译器不行, 我试了两种编译器, 你的代码都没问题, 除了cou<<endl;
taurusbbstech 2008-03-28
  • 打赏
  • 举报
回复
谢谢大家关于
cout << endl;
的回复,我最想搞清楚的是重载解析.
ppzzhhsky 2008-03-28
  • 打赏
  • 举报
回复
template <typename T>
void ShowArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
for (int i = 0; i < n; i++)
cout << *arr[i] << ' ';
cout endl;
}
该为
template <typename T>
void ShowArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
for (int i = 0; i < n; i++)
cout << *arr[i] << ''<<endl;

}
homesos 2008-03-28
  • 打赏
  • 举报
回复
看错了,我说错了!

LZ的模板函数还是重新设计一下吧,typename T类型差不多,容易出错。
homesos 2008-03-28
  • 打赏
  • 举报
回复
typename T类型是差不多的,一个是int,一个是double。
因为变量你没有加显式的标识,所以程序处理的时候分不开,哪个是int,哪个是double。
taurusbbstech 2008-03-28
  • 打赏
  • 举报
回复
还是不太明白,pd不就是double *的数组吗?
homesos 2008-03-28
  • 打赏
  • 举报
回复

①ShowArray<double>(pd, 3);
②cout<<endl;
taurusbbstech 2008-03-28
  • 打赏
  • 举报
回复
呵呵..加上去了.可是还是出错.

'ShowArray' : none of 2 overload have a best conversion
'ShowArray' : ambiguous call to overloaded function
homesos 2008-03-28
  • 打赏
  • 举报
回复

double * pd[3];
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
//pd没有初始化,就直接使用


ShowArray(pd, 3);
//是这样调用模板函数吗?
louise365 2008-03-28
  • 打赏
  • 举报
回复
最后的cout少了<<
taurusbbstech 2008-03-28
  • 打赏
  • 举报
回复
编译不过,帮我看看,谢谢
Proteas 2008-03-28
  • 打赏
  • 举报
回复
cou<<endl;
?
映维网 2008-03-28
  • 打赏
  • 举报
回复
buihuiba

64,651

社区成员

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

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