免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4158 | 回复: 4
打印 上一主题 下一主题

[已解决]帮看下这个模板类为什么不能编译(codeblocks) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-28 21:32 |只看该作者 |倒序浏览
本帖最后由 thefirstz 于 2012-01-07 11:20 编辑
  1. struct __xtrue_type { }; // define two mark-type
  2. struct __xfalse_type { };

  3. class CComplexObject{
  4. public:
  5.      virtual void clone() { cout << "in clone" << endl; }
  6. };

  7. class CDerivedComplexObject : public CComplexObject {
  8. public:
  9.      virtual void clone() { cout << "in derived clone" << endl; }
  10. };

  11. template <typename T>
  12. struct Traits{  typedef __xfalse_type has_clone_method;};
  13. template <>
  14. struct Traits<CComplexObject>{  typedef __xtrue_type has_clone_method;};

  15. template <typename T>
  16. class XContainer{
  17. public:
  18.     template <typename flag, typename N=void>
  19.     class Impl { };
  20.     template <typename N>
  21.     class Impl <__xtrue_type,N>
  22.     {
  23.     public:
  24.         void clone(T* pObj)  {  pObj->clone();    }
  25.     };
  26.     template <typename N>
  27.     class Impl <__xfalse_type,N>
  28.     {
  29.     public:
  30.         void clone(T* pObj) {  }
  31.     };
  32. public:
  33.     void clone(T* pObj){ Impl< Traits<T>::has_clone_method >().clone(pObj);  }
  34. };
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-28 22:54 |只看该作者
本帖最后由 liwangli1983 于 2011-12-28 22:55 编辑

因为没有帖出错误信息和更多的代码,单从给出的代码来看错误有可能出在下面这个地方:

    void clone(T* pObj){ Impl< Traits<T>::has_clone_method >().clone(pObj);  }
改为:
    void clone(T* pObj){ Impl< typename Traits<T>::has_clone_method >().clone(pObj);  }

Impl的模板参数显然需要的是一个类型,但不显式的用typename指明的情况下,Traits<T>::has_clone_method会被认为是数据成员(而不是类型成员)。

论坛徽章:
0
3 [报告]
发表于 2011-12-28 22:58 |只看该作者
  1. #include<iostream>
  2. using std::cout;
  3. using std::endl;
  4. struct __xtrue_type { }; // define two mark-type
  5. struct __xfalse_type { };

  6. class CComplexObject{
  7. public:
  8.      virtual void clone() { cout << "in clone" << endl; }
  9. };

  10. class CDerivedComplexObject : public CComplexObject {
  11. public:
  12.      virtual void clone() { cout << "in derived clone" << endl; }
  13. };

  14. template <typename T>
  15. struct Traits{  typedef __xfalse_type has_clone_method;};
  16. template <>
  17. struct Traits<CComplexObject>{  typedef __xtrue_type has_clone_method;};

  18. template <typename T>
  19. class XContainer{
  20. public:
  21.     template <typename flag, typename N=void>
  22.     class Impl { };
  23.     template <typename N>
  24.     class Impl <__xtrue_type,N>
  25.     {
  26.     public:
  27.         void clone(T* pObj)  {  pObj->clone();    }
  28.     };
  29.     template <typename N>
  30.     class Impl <__xfalse_type,N>
  31.     {
  32.     public:
  33.         void clone(T* pObj) {  }
  34.     };
  35. public:
  36.     void clone(T* pObj){ Impl< typename Traits<T>::has_clone_method >().clone(pObj);  }
  37. };

  38. int main()
  39. {
  40.         XContainer<CComplexObject> a;
  41.         CComplexObject k;
  42.         CDerivedComplexObject j;
  43.         a.clone(&k);
  44.         a.clone(&j);
  45.         return 0;
  46. }
复制代码
补上一个简单的main后,编译成功,运行结果如下:
  1. ???@atom:~$ ./a.out
  2. in clone
  3. in derived clone
复制代码

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
4 [报告]
发表于 2011-12-28 22:59 |只看该作者
第一,Code::Blocks是个IDE,而非编译器。例如,你应该说“帮看下这个模板类为什么不能编译(MinGW4.6.1)”
第二,不能编译通过,应该贴出编译器给出的错误提示
例如 error: 'cout' was not declared in this scope
必然就知道你少写了
#include <iostream>
using namespace std;

例如 void clone(T* pObj){ Impl< Traits<T>::has_clone_method >().clone(pObj);  } 处报错
error: type/value mismatch at argument 1 in template parameter list for 'template<class T> template<class flag, class N> class XContainer<T>::Impl'
error:   expected a type, got 'Traits<T>::has_clone_method'
别人就知道 Impl< Traits<T>::has_clone_method > 应该写成 Impl< typename Traits<T>::has_clone_method >

例如 undefined reference to `WinMain@16'
必然就知道你少写了
int main()
{
}

论坛徽章:
1
午马
日期:2013-09-10 11:03:08
5 [报告]
发表于 2011-12-29 09:37 |只看该作者
又是一个不用main的人
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP