花开自有丶花落 发表于 2016-8-3 20:30:08

模板具体化怎么定义,看不出哪里出错了

sumarray具体化有问题求助


#include <iostream>
#include <cstdlib>
using namespace std;
struct debts
{
       char name;
       double amount;
};
template<>double sumarray<debts>(debts ar[],int n);
template<class T>
T sumarray(T ar[],int n);
int main()
{
    int things = {13,31,103,301,310,130};
    debts ded =
    {
         {"Ima Wlofe",2400.0},
         {"Ura Foxe",1300.0},
         {"Iby Stout",1800.0}
    };
    double *pd;
    for(int i = 0;i<3;i++)
    {
            pd = &ded.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;
      for(int i = 1;i<n;i++)
      re+=ar;
      return re;
}
template <> double sumarray(debts ar[],int n)
{
       double re = ar.amount;
       for(int i = 1;i<n;i++)
       re = re+ar.amount;
       return re;
}

patton88 发表于 2016-8-3 20:30:09

本帖最后由 patton88 于 2016-8-3 21:34 编辑

// 前置声明方式也行

// VS2013 Win32 Console UNICODE Success Run

#include <iostream>
#include <cstdlib>
using namespace std;

struct debts
{
        char name;
        double amount;
};

template<class T>
double sumarray(T ar[], int n);

template<>
double sumarray<debts>(debts ar[], int n);

int main()
{
        int things = { 13, 31, 103, 301, 310, 130 };
        debts ded =
        {
                { "Ima Wlofe", 2400.0 },
                { "Ura Foxe", 1300.0 },
                { "Iby Stout", 1800.0 }
        };
        double *pd;
        for (int i = 0; i < 3; i++)
        {
                pd = &ded.amount;
        }
        cout << "things zong he:" << sumarray(things, 6) << endl;
        cout << "ded zong he:" << sumarray(ded, 3) << endl;
        system("pause");
        return 0;
}

template<class T>
double sumarray(T ar[], int n)
{
        double re = ar;
        for (int i = 1; i < n; i++)
                re += ar;
        return re;
}

template<>
double sumarray(debts ar[], int n)
{
        double re = ar.amount;
        for (int i = 1; i < n; i++)
                re = re + ar.amount;
        return re;
}


/* Result of Run:

things zong he:888
ded zong he:5500
Press any key to continue . . .

*/

patton88 发表于 2016-8-3 21:14:31

本帖最后由 patton88 于 2016-8-3 21:34 编辑

// VS2013 Win32 Console UNICODE Success Run

#include <iostream>
#include <cstdlib>
using namespace std;

struct debts
{
        char name;
        double amount;
};

template<class T>
double sumarray(T ar[], int n)
{
        double re = ar;
        for (int i = 1; i < n; i++)
                re += ar;
        return re;
}

template<>
double sumarray(debts ar[], int n)
{
        double re = ar.amount;
        for (int i = 1; i < n; i++)
                re = re + ar.amount;
        return re;
}

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

        debts ded =
        {
                { "Ima Wlofe", 2400.0 },
                { "Ura Foxe", 1300.0 },
                { "Iby Stout", 1800.0 }
        };

        double *pd;
       
        for (int i = 0; i < 3; i++)
        {
                pd = &ded.amount;
        }
       
        cout << "things zong he:" << sumarray(things, 6) << endl;
        cout << "ded zong he:" << sumarray(ded, 3) << endl;

        system("pause");
        return 0;
}

/* Result of Run:

things zong he:888
ded zong he:5500
Press any key to continue . . .

*/

花开自有丶花落 发表于 2016-8-6 17:00:14

patton88 发表于 2016-8-3 21:14


前几天没看,试了一下编译过了,就给你,刚刚看了,这个程序的问题是函数模板的返回类型不能用T代替吧?谢谢啦

patton88 发表于 2016-8-6 19:45:04

花开自有丶花落 发表于 2016-8-6 17:00
前几天没看,试了一下编译过了,就给你,刚刚看了,这个程序的问题是函数模板的返回类型不能用T代替吧? ...

多交流,共同学习

花开自有丶花落 发表于 2016-8-6 20:08:24

patton88 发表于 2016-8-6 19:45
多交流,共同学习

{:9_232:}

yintotti 发表于 2016-8-7 10:06:15

看不懂
页: [1]
查看完整版本: 模板具体化怎么定义,看不出哪里出错了