sumarray具体化有问题求助
#include <iostream>
#include <cstdlib>
using namespace std;
struct debts
{
char name[50];
double amount;
};
template<>double sumarray<debts>(debts ar[],int n);
template<class T>
T sumarray(T ar[],int n);
int main()
{
int things[6] = {13,31,103,301,310,130};
debts ded[3] =
{
{"Ima Wlofe",2400.0},
{"Ura Foxe",1300.0},
{"Iby Stout",1800.0}
};
double *pd[3];
for(int i = 0;i<3;i++)
{
pd[i] = &ded[i].amount;
}
cout<<"things zong he:"<<sumarray(things,6)<<endl;
cout<<"ded zong he:"<<sumarray(ded,3)<<endl;
system("pause");
return 0;
}
template<class T>
T sumarray(T ar[],int n)
{
T re = ar[0];
for(int i = 1;i<n;i++)
re+=ar[i];
return re;
}
template <> double sumarray(debts ar[],int n)
{
double re = ar[0].amount;
for(int i = 1;i<n;i++)
re = re+ar[i].amount;
return re;
}
|