| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 582 人关注过本帖
标题:书上的例程,为什么我编译总出现错误
只看楼主 加入收藏
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
 问题点数:0 回复次数:3 
书上的例程,为什么我编译总出现错误

// tempover.cpp --- template overloading
#include <iostream>

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

template <typename T> // template B
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.0},
{"Ura Foxe", 1300.0},
{"Iby Stout", 1800.0}
};
double * pd[3];

// set pointers to the amount members of the structures in the arr mr_E
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;

cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
ShowArray(things, 6); // uses template A
cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
ShowArray(pd, 3); // uses template B (more specialized)
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;
}
这是c++ primer plus(第五版)的一道例题(程序8.14),可是我怎么编译都是不通过,我的编译器是VC6.0。错误原因是
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;
}
无法匹配,你们会出现这样的问题吗?
error C2667: 'ShowArray' : none of 2 overload have a best conversion
error C2668: 'ShowArray' : ambiguous call to overloaded function

搜索更多相关主题的帖子: 例程 编译 
2007-08-14 17:24
neverDie
Rank: 1
等 级:新手上路
威 望:1
帖 子:123
专家分:0
注 册:2007-5-5
收藏
得分:0 

是不能重载吗?看字面!


2007-08-14 18:05
terisevend
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-6-2
收藏
得分:0 
DEV-C++上编译通过!
VS2005编译通过!

2007-08-14 20:06
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 

为什么会这样。为什么VC6上不行。是编译器的问题???

2007-08-14 20:09
快速回复:书上的例程,为什么我编译总出现错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013765 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved