免费注册 查看新帖 |

Chinaunix

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

[C++] C++ 模板编译错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-08 11:37 |只看该作者 |倒序浏览
这是一个模板类,实现最简单的二叉查找树,报错如下:

bst.cpp: In member function ‘void BinarySearchTree<T>::makeEmpty() const [with T = char]’:
bst.cpp:19:   instantiated from ‘BinarySearchTree<T>::~BinarySearchTree() [with T = char]’
bst.cpp:154:   instantiated from here
bst.cpp:29: error: no matching function for call to ‘BinarySearchTree<char>::makeEmpty(BinarySearchTree<char>::BinaryNode* const&) const’
bst.cpp:29: note: candidates are: void BinarySearchTree<T>::makeEmpty() const [with T = char]
bst.cpp:129: note:                 void BinarySearchTree<T>::makeEmpty(BinarySearchTree<T>::BinaryNode*&) const [with T = char]



代码
  1. #include <iostream>
  2. #include <string>

  3. template <class T>
  4. class BinarySearchTree
  5. {
  6.         public:
  7.                 BinarySearchTree () : root (NULL) { }
  8.                 BinarySearchTree (const BinarySearchTree &rhs) { root = clone  (rhs.root); }
  9.                 const BinarySearchTree &operator= (const BinarySearchTree &rhs)
  10.                 {
  11.                         if (this != &rhs)
  12.                         {
  13.                                 makeEmpty ();
  14.                                 root = clone (rhs.root);
  15.                         }
  16.                         return *this;
  17.                 }
  18.                 ~BinarySearchTree () {        makeEmpty (); }

  19.                 void insert (const T &x) { insert (x, root); }
  20.                 void remove (const T &x) { remove (x, root); }

  21.                 const T &findMax () const { findMax (root); }
  22.                 const T &findMin () const { findMin (root); }

  23.                 bool contains (const T &x) const { return container (x, root); }
  24.                 bool isEmpty () const { return (root != NULL); }
  25.                 void makeEmpty () const { makeEmpty (root); }
  26.                 void printTree () const { printTree (root); }
  27.                
  28.         private:
  29.                 struct BinaryNode
  30.                 {
  31.                         T data;
  32.                         BinaryNode *left;
  33.                         BinaryNode *right;
  34.                         BinaryNode (const T &elem, BinaryNode *lt, BinaryNode *rt) : data (elem), left (0), right (0) { }
  35.                 };

  36.                 BinaryNode *root;
  37.                 void insert (const T &x, BinaryNode *&t) const;
  38.                 void remove (const T &x, BinaryNode *&t) const;
  39.                 BinaryNode *findMax (BinaryNode *t) const;
  40.                 /*
  41.                 {
  42.                         if (t)
  43.                                 while (t->right)
  44.                                         t = t->right;
  45.                         return t;
  46.                 }*/
  47.                 BinaryNode *findMin (BinaryNode *t) const
  48.                 {
  49.                         if (t == NULL)
  50.                                 return NULL;
  51.                         if (t->left == NULL)
  52.                                 return NULL;
  53.                         return findMin (t->left);
  54.                 }
  55.                 BinaryNode *clone (BinaryNode *t) const
  56.                 {
  57.                         if (t == NULL)
  58.                                 return NULL;
  59.                         return new BinaryNode (t->data, clone(t->left), clone(t->right));
  60.                 }
  61.                 bool contains (const T &x, BinaryNode *t) const;
  62.                 void makeEmpty (BinaryNode *&t) const;
  63.                 void printTree (BinaryNode *t) const;

  64. };

  65. template <class T>
  66. void BinarySearchTree<T>::insert (const T &x, BinaryNode *&t) const
  67. {
  68.         if (t == NULL)
  69.                 t = new BinaryNode (x, NULL, NULL);
  70.         else if (x < t->data)
  71.                 insert (x, t->left);
  72.         else if (x > t->data)
  73.                 insert (x, t->right);
  74.         else
  75.                 ;//same value , do nothing
  76. }

  77. template <class T>
  78. void BinarySearchTree<T>::remove (const T &x, BinaryNode *&t) const
  79. {
  80.         if (t == NULL)
  81.                 return;
  82.         if (x < t->data)
  83.                 remove (x, t->left);
  84.         else if (x > t->data)
  85.                 remove (x, t->right);
  86.         else if (t->left && t->right)        //Two children
  87.         {
  88.                 t->data = findMin (t->right)->data;
  89.                 remove(t->data, t->right);
  90.         }
  91.         else
  92.         {
  93.                 BinaryNode *oldNode = t;
  94.                 t = (t->left) ? t->left : t->right;
  95.                 delete t;
  96.         }
  97. }
  98. template <class T>
  99. struct BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMax (BinaryNode *t) const
  100. {
  101.         if (t)
  102.                 while (t->right)
  103.                         t=t->right;
  104.         return t;
  105. }


  106. template <class T>
  107. bool BinarySearchTree<T>::contains (const T &x, BinaryNode *t) const
  108. {
  109.         if (t == NULL)
  110.                 return false;
  111.         else if (x < t->data)
  112.                 return contains (x, t->left);
  113.         else if (x > t->data)
  114.                 return contains (x, t->right);
  115.         return true;
  116. }

  117. template <class T>
  118. void BinarySearchTree<T>::makeEmpty (BinaryNode *&t) const
  119. {
  120.         if (t != NULL)
  121.         {
  122.                 makeEmpty (t->left);
  123.                 makeEmpty (t->right);
  124.                 delete t;
  125.         }
  126.         t = NULL;        //
  127. }

  128. template <class T>
  129. void BinarySearchTree<T>::printTree (BinaryNode *t) const
  130. {
  131.         std::cout << "LNR" << std::endl;
  132.         if (t != NULL)
  133.         {
  134.                 printTree (t->left);
  135.                 std::cout << " " << t->data;
  136.                 printTree (t->right);
  137.         }
  138. }

  139. int main ()
  140. {
  141.         BinarySearchTree<char> bst;
  142.         bst.insert ('F');
  143.         bst.insert ('B');
  144.         bst.insert ('G');
  145.         bst.insert ('A');
  146.         bst.insert ('D');
  147.         bst.insert ('I');
  148.         bst.insert ('C');
  149.         bst.insert ('E');
  150.         bst.insert ('H');
  151.         //bst.isEmpty ();

  152.         bst.printTree ();
  153.         return 0;
  154. }
复制代码

论坛徽章:
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 [报告]
发表于 2012-08-08 12:37 |只看该作者
struct BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMax (BinaryNode *t) const
改为
typename BinarySearchTree<T>::BinaryNode* BinarySearchTree<T>::findMax( BinaryNode* t ) const

void makeEmpty () const { makeEmpty (root); }
改为
void makeEmpty() { makeEmpty(root); }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP