标题:C++中函数模板显示具体化问题大家快来看看啊~(已解决)
取消只看楼主
沿途有鬼
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2008-7-20
 问题点数:0 回复次数:1 
C++中函数模板显示具体化问题大家快来看看啊~(已解决)
#include<iostream>
using namespace std;

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



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


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

int main(void)
{
    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];

    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 <class T>
void ShowArray(T arr[],int n)
{
    T sum=0;
    cout<<"template A\n";
    for(int i=0;i<n;i++)
        sum=sum+arr[i];//1有错
    cout<<"Sum things are "<<sum<<endl<<endl;
}

template <> void ShowArray<char *>(char * arr[],int n)
{
    double sum=0;
        cout<<"template B\n";
            for(int i=0;i<n;i++)
        sum=sum+*arr[i];
            cout<<"Sum debts are "<<sum<<endl<<endl;
}



        

请帮我把程序改正确,能够指点一下更加感谢啊~

[[it] 本帖最后由 沿途有鬼 于 2008-8-5 18:08 编辑 [/it]]
搜索更多相关主题的帖子: 中函数 模板 具体化 std void 
2008-07-28 21:23
沿途有鬼
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2008-7-20
得分:0 
非常感谢3楼,4楼,7楼的回答啊,现在我弄懂了~

特别感谢zerocn的超级详细解答,让我晃然大悟啊~
2008-08-05 10:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-225620-1-1.html




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

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