| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 711 人关注过本帖
标题:帮满修改下!
只看楼主 加入收藏
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
结帖率:95%
收藏
已结贴  问题点数:20 回复次数:8 
帮满修改下!
程序代码:
#include<iostream>
using namespace std;
template<class any>
showarray(any array[],int n);

template<class any>
showarray(any *array[],int n);

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

int main(void)
{
int things[6]={13,31,103,301,310,130};
struct debts mr_e[3]=
{
{"I am A",240.0},
{"I am B",120.0},
{"I am C",150.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 any>
showarray(any array[],int n)
{
float sum;
cout<<"template A\n";
for(int i=0;i<n;i++)
sum+=arrary[i];
cout<<"sum="<<sum;
cout<<endl;
return sum;
}

template<class any>
showarray(any *array[],int n)
{
float sum;
cout<<"template A\n";
for(int i=0;i<n;i++)
sum+=*arrary[i];
cout<<"sum="<<sum;
cout<<endl;
return sum;
}
搜索更多相关主题的帖子: std color 
2010-03-12 11:49
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
收藏
得分:0 
我想用模板函数,然后输出数组的和,编译有一个错误,帮改下
2010-03-12 16:08
chengwen1016
Rank: 2
等 级:论坛游民
帖 子:14
专家分:60
注 册:2010-3-6
收藏
得分:10 
回复 楼主 最近不在
修改如下:

#include<iostream>
using namespace std;

template<class any>
showarray(any array[],int n);

template<class any>
showarray(any *array[],int n);

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

int main(void)
{
    int things[6]={13,31,103,301,310,130};
    struct debts mr_e[3]=
    {
        {"I am A",240.0},
        {"I am B",120.0},
        {"I am C",150.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);  // 这里传参数要传 *pd
    return 0;
}

template<class any>
showarray(any array[],int n)
{
    float sum = 0;     // 要赋初值 0, 否则得不到正确结果的,因为 sum 的初值为一个任意数
    cout<<"template A\n";
    for(int i=0;i<n;i++)
        sum+=array[i];
    cout<<"sum="<<sum;
    cout<<endl;
    return sum;
}

template<class any>
showarray(any *array[],int n)
{
    float sum = 0;   // 要赋初值 0
    cout<<"template A\n";
    for(int i=0;i<n;i++)
        sum+=*array[i];
    cout<<"sum="<<sum;
    cout<<endl;
    return sum;
}
2010-03-12 18:48
ltyjyufo
Rank: 9Rank: 9Rank: 9
来 自:未来
等 级:蜘蛛侠
威 望:2
帖 子:353
专家分:1166
注 册:2009-10-25
收藏
得分:0 
同意3楼的修改,就是在showarray(any array[],int n)和showarray(any *array[],int n)函数这里,楼主应该指明他的返回类型,这样写规范一定嘛。。。。。。。

翱翔天空的雄鹰固然令人羡慕,却容易被禁锢于牢笼之中,只有那夜色中的蝙蝠才是真正自由的飞翔者....
2010-03-12 20:42
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
收藏
得分:0 
谢谢2楼了,3楼貌似看跑了,呵呵
2010-03-13 00:00
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
收藏
得分:0 
我还想问下,就是第二个的和求的不对,再该怎么改了?
2010-03-13 00:03
杨三星
Rank: 1
来 自:河南
等 级:新手上路
帖 子:8
专家分:5
注 册:2010-3-13
收藏
得分:0 
呵呵
2010-03-13 09:48
cxxcomp
Rank: 2
等 级:论坛游民
帖 子:3
专家分:15
注 册:2010-2-6
收藏
得分:5 
程序代码:
#include<iostream>
using namespace std;

template<typename T>
double showarray(T* array, int n);

template<typename T>
double showarray(T** array, int n);

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

int main(void)
{
    int things[6]={13,31,103,301,310,130};

    struct debts mr_e[3]=
    {
        {
            "I am A",
            240.0
        },
        {
            "I am B",
            120.0
        },
        {
            "I am C",
            150.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) << endl;
    cout<<"listing mr_e's debts: \n" << showarray(pd,3) << endl;

    return 0;
}

template<typename T>
double showarray(T* array,int n)
{
    double sum = 0;
    cout<<"template A\n";
    for(int i = 0; i < n; i++)
        sum += array[i];
    cout << "sum=" << sum << endl;

    return sum;
}

template<typename T>
double showarray(T **array,int n)
{
    double sum = 0;
    cout<<"template A\n";
    for(int i = 0; i < n; i++)
        sum += *array[i];
    cout<< "sum=" << sum << endl;

    return sum;
}
第二个showarray计算结果510.难道不对么?
2010-03-13 22:14
chengwen1016
Rank: 2
等 级:论坛游民
帖 子:14
专家分:60
注 册:2010-3-6
收藏
得分:5 
从运行结果可以知道,其实程序根本就没有调用B模板函数 所以结果会错了  其实改一下用一个函数就可以了

#include<iostream>
using namespace std;

template<class any>
showarray(any array[],int n);


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

int main(void)
{
    int things[6]={13,31,103,301,310,130};
    struct debts mr_e[3]=
    {
        {"I am A",240.0},
        {"I am B",120.0},
        {"I am C",150.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 any>
showarray(any array[],int n)
{
    float sum = 0;     
    cout<<"template A\n";
    for(int i=0;i<n;i++)
        sum+=array[i];
    cout<<"sum="<<sum;
    cout<<endl;
    return sum;
}
2010-03-13 22:44
快速回复:帮满修改下!
数据加载中...
 
   



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

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