鱼C论坛

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

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

[复制链接]
发表于 2020-9-19 11:52:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 stevenmtroy 于 2020-9-19 13:05 编辑

请问这样写为什么编译器总报错:不可识别的模板定义/声明?
#include <iostream>

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

template <typename T>
class Test {
    T a;
    T b;
public:
    Test(int a, int b)
    {
        this->a = a;
        this->b = b;
    }
    void Print()const
    {
        std::cout << a << " , " << b;
    }

    template <typename T>
    friend Test<T> operator++(Test<T>& t);
};

int main()
{
    Test<int> test(0, 0);
    (++test).Print();
}

template <typename T> Test<T> operator++(Test<T>& t)
{
    return Test<T>(++t.a, ++t.b);
}
最佳答案
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. //}
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-19 12:07:18 | 显示全部楼层
本帖最后由 昨非 于 2020-9-19 12:11 编辑

抱歉看错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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. //}
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-20 16:50:47 | 显示全部楼层
我已经调出来了, 不过还是要感谢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 19:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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