鱼C论坛

 找回密码
 立即注册
查看: 1063|回复: 3

[已解决]模板的前置声明

[复制链接]
发表于 2020-9-20 00:00:35 | 显示全部楼层    本楼为最佳答案   
是要实现++操作符的重载吗?
  1. #include <iostream>

  2. //template <typename T> Test<T> operator++(Test<T>& t);

  3. template <typename T>
  4. class Test {
  5.     T a;
  6.     T b;
  7. public:
  8.     Test(T a, T b)
  9.     {
  10.         this->a = a;
  11.         this->b = b;
  12.     }

  13.         Test<T> & operator ++()
  14.         {
  15.                 a++;
  16.                 b++;
  17.                 return * this;
  18.         }

  19.     void Print()const
  20.     {
  21.         std::cout << a << " , " << b;
  22.     }
  23.        
  24. //    template <typename T>
  25. //                friend Test<T> operator++(Test<T>& t);
  26. };

  27. int main()
  28. {
  29.     Test<int> test(0, 0);
  30.         (++test).Print();
  31.     return 0;
  32. }

  33. //template <typename T> Test<T> operator++(Test<T>& t)
  34. //{
  35. //   return Test<T>(++t.a, ++t.b);
  36. //}
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-30 08:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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