免费注册 查看新帖 |

Chinaunix

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

[C++] 模板类问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-12-18 09:43 |只看该作者 |倒序浏览
1.GetRoot() 方法报错信息如下。

1>------ Build started: Project: Demo, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\administrator\desktop\demo\demo\binarysearchtree.h(41): error C2143: syntax error : missing ';' before '*'
1>c:\users\administrator\desktop\demo\demo\binarysearchtree.h(41): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\administrator\desktop\demo\demo\binarysearchtree.h(41): error C2065: 'Key' : undeclared identifier
1>c:\users\administrator\desktop\demo\demo\binarysearchtree.h(41): error C2065: 'Val' : undeclared identifier
1>c:\users\administrator\desktop\demo\demo\binarysearchtree.h(41): error C2923: 'IBSTree' : 'Key' is not a valid template type argument for parameter 'Key'
1>c:\users\administrator\desktop\demo\demo\binarysearchtree.h(41): error C2923: 'IBSTree' : 'Val' is not a valid template type argument for parameter 'Val'


个人觉得是找不到Node的定义,但是如果是函数参数中Node就不会报错。请高手指点。说明具体原因和修改方法。
多谢。

上代码。
  1. #ifndef _BINARY_SEARCH_TREE_H_
  2. #define _BINARY_SEARCH_TREE_H_

  3. template <typename Key, typename Val>
  4. class IBSTree
  5. {
  6. private:
  7.         struct Node
  8.         {
  9.                 Key k;
  10.                 Val v;
  11.                 Node *parent;
  12.                 Node *left;
  13.                 Node *right;
  14.         };
  15.         Node *m_pRoot;

  16.         typedef int (*TCmpFunc)(Key k1, Key k2);
  17.         TCmpFunc m_pCmpFunc;


  18. public:
  19.         IBSTree();
  20.         ~IBSTree();
  21.        
  22.         Node *GetRoot();
  23. };


  24. template <typename Key, typename Val>
  25. Node *IBSTree<Key, Val>::GetRoot()
  26. {
  27.         return m_pRoot;
  28. }

  29. template <typename Key, typename Val>
  30. IBSTree<Key, Val>::IBSTree()
  31. {
  32.         m_pRoot = NULL;
  33.         m_pCmpFunc = NULL;
  34. }

  35. template <typename Key, typename Val>
  36. IBSTree<Key, Val>::~IBSTree()
  37. {
  38.         m_pRoot = NULL;
  39.         m_pCmpFunc = NULL;
  40. }

  41. #endif
复制代码

论坛徽章:
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
2 [报告]
发表于 2014-12-18 10:18 |只看该作者
Node *IBSTree<Key, Val>::GetRoot()
改为
typename IBSTree<Key,Val>::Node* IBSTree<Key, Val>::GetRoot()

论坛徽章:
0
3 [报告]
发表于 2014-12-18 14:20 |只看该作者
回复 2# bruceteen


多谢。 但是还有一点疑问为什么如果这么写就不需要在Node前面加 typename IBSTree<Key, Val>::  了呢。


代码如下
  1. template <typename Key, typename Val>
  2. int IBSTree<Key, Val>::GetRoot(Node *pNode)
  3. {
  4.         pNode = m_pRoot;
  5. }
复制代码

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
4 [报告]
发表于 2014-12-18 16:37 |只看该作者
回复 3# illidanee

因为作为参数将使的ADL(Argument-dependent lookup)起作用,自动包含了类型所在的名字空间,而作为函数返回值的话就需要明确其符号名称的范围了。
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP