鱼C论坛

 找回密码
 立即注册
查看: 1923|回复: 6

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

[复制链接]
发表于 2016-8-3 20:30:08 | 显示全部楼层 |阅读模式
20鱼币
sumarray具体化有问题求助


  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. struct debts
  5. {
  6.        char name[50];
  7.        double amount;
  8. };
  9. template<>double sumarray<debts>(debts ar[],int n);
  10. template<class T>
  11. T sumarray(T ar[],int n);
  12. int main()
  13. {
  14.     int things[6] = {13,31,103,301,310,130};
  15.     debts ded[3] =
  16.     {
  17.            {"Ima Wlofe",2400.0},
  18.            {"Ura Foxe",1300.0},
  19.            {"Iby Stout",1800.0}
  20.     };
  21.     double *pd[3];
  22.     for(int i = 0;i<3;i++)
  23.     {
  24.             pd[i] = &ded[i].amount;
  25.     }
  26.     cout<<"things zong he:"<<sumarray(things,6)<<endl;
  27.     cout<<"ded zong he:"<<sumarray(ded,3)<<endl;
  28.     system("pause");
  29.     return 0;
  30. }
  31. template<class T>
  32. T sumarray(T ar[],int n)
  33. {
  34.       T re = ar[0];
  35.       for(int i = 1;i<n;i++)
  36.       re+=ar[i];
  37.       return re;
  38. }
  39. template <> double sumarray(debts ar[],int n)
  40. {
  41.        double re = ar[0].amount;
  42.        for(int i = 1;i<n;i++)
  43.        re = re+ar[i].amount;
  44.        return re;
  45. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-3 20:30:09 | 显示全部楼层
本帖最后由 patton88 于 2016-8-3 21:34 编辑
  1. // 前置声明方式也行

  2. // VS2013 Win32 Console UNICODE Success Run

  3. #include <iostream>
  4. #include <cstdlib>
  5. using namespace std;

  6. struct debts
  7. {
  8.         char name[50];
  9.         double amount;
  10. };

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

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

  15. int main()
  16. {
  17.         int things[6] = { 13, 31, 103, 301, 310, 130 };
  18.         debts ded[3] =
  19.         {
  20.                 { "Ima Wlofe", 2400.0 },
  21.                 { "Ura Foxe", 1300.0 },
  22.                 { "Iby Stout", 1800.0 }
  23.         };
  24.         double *pd[3];
  25.         for (int i = 0; i < 3; i++)
  26.         {
  27.                 pd[i] = &ded[i].amount;
  28.         }
  29.         cout << "things zong he:" << sumarray(things, 6) << endl;
  30.         cout << "ded zong he:" << sumarray(ded, 3) << endl;
  31.         system("pause");
  32.         return 0;
  33. }

  34. template<class T>
  35. double sumarray(T ar[], int n)
  36. {
  37.         double re = ar[0];
  38.         for (int i = 1; i < n; i++)
  39.                 re += ar[i];
  40.         return re;
  41. }

  42. template<>
  43. double sumarray(debts ar[], int n)
  44. {
  45.         double re = ar[0].amount;
  46.         for (int i = 1; i < n; i++)
  47.                 re = re + ar[i].amount;
  48.         return re;
  49. }


  50. /* Result of Run:

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

  54. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-3 21:14:31 | 显示全部楼层
本帖最后由 patton88 于 2016-8-3 21:34 编辑
  1. // VS2013 Win32 Console UNICODE Success Run

  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;

  5. struct debts
  6. {
  7.         char name[50];
  8.         double amount;
  9. };

  10. template<class T>
  11. double sumarray(T ar[], int n)
  12. {
  13.         double re = ar[0];
  14.         for (int i = 1; i < n; i++)
  15.                 re += ar[i];
  16.         return re;
  17. }

  18. template<>
  19. double sumarray(debts ar[], int n)
  20. {
  21.         double re = ar[0].amount;
  22.         for (int i = 1; i < n; i++)
  23.                 re = re + ar[i].amount;
  24.         return re;
  25. }

  26. int main()
  27. {
  28.         int things[6] = { 13, 31, 103, 301, 310, 130 };

  29.         debts ded[3] =
  30.         {
  31.                 { "Ima Wlofe", 2400.0 },
  32.                 { "Ura Foxe", 1300.0 },
  33.                 { "Iby Stout", 1800.0 }
  34.         };

  35.         double *pd[3];
  36.        
  37.         for (int i = 0; i < 3; i++)
  38.         {
  39.                 pd[i] = &ded[i].amount;
  40.         }
  41.        
  42.         cout << "things zong he:" << sumarray(things, 6) << endl;
  43.         cout << "ded zong he:" << sumarray(ded, 3) << endl;

  44.         system("pause");
  45.         return 0;
  46. }

  47. /* Result of Run:

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

  51. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-8-6 17:00:14 | 显示全部楼层

前几天没看,试了一下编译过了,就给你,刚刚看了,这个程序的问题是函数模板的返回类型不能用T代替吧?谢谢啦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

多交流,共同学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-8-6 20:08:24 | 显示全部楼层
patton88 发表于 2016-8-6 19:45
多交流,共同学习

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-7 10:06:15 | 显示全部楼层
看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-13 20:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表