注册 登录
编程论坛 C++教室

模板重载问题

zzt_428 发布于 2008-09-09 05:15, 739 次点击
麻烦各位看一下下面的程序.请多多指点.
douboe *pd[3];
//...
Show(pd,3);             //为什么不能与下面的Show(T *arr[], int n) 匹配?
//....
.........
template <typename T>
void Show(T *arr[], int n)
{
   //....
}

////////////////////////////////////////////////////////
完整的程序如下:

#include <iostream>
using namespace std;

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

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

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

int main(void)
{
    int things[6]={301,302,303,304,305,306};
    struct debts debtsArr[3]=
    {
        {"Micheal Zhuang",2500.0},
        {"Echo She",2600.0},
        {"James He",2300.0}
    };

    double *pd[3];
    for(int i=0; i<3; i++)
        pd[i]=&debtsArr[i].amount;

    Show(things,6);

         cout << "Count infomation:\n";
    Show(pd,3);
    
    return 0;
}

template <typename T>
void Show(T arr[], int n)
{
    cout << "Using template A:\n";
    for(int i=0; i<n; i++)
        cout << arr[i] << "  ";
    cout << endl;
}

template <typename T>
void Show(T *arr[], int n)
{
    cout << "Using template B:\n";
    for(int i=0; i<n; i++)
        cout << *arr[i] << " ";
    cout << endl;
}
4 回复
#2
zzt_4282008-09-09 05:22
###
编译器版本太低!
Show(pd,3);

void Show(T arr[], int n);
void Show(T *arr[], int n);
均能进行匹配,因此出现歧义.
在高级版本的编译器中,void Show(T *arr[], int n)是最佳匹配,编译可以通过。
#3
blueboy820062008-09-09 17:49
自问自答!!
#4
yefeng_laixi2008-09-09 18:24
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#5
fcyvhk2008-09-09 23:59
学习了,我第一遍看的时候想当然就认为pd应该就传*arr[] 没想到歧义。
1