作者lleyfede (ARod)
看板EE_DSnP
标题[问题] 在iterator里access到BStree的member
时间Wed Dec 9 13:15:07 2009
为了取得BSTree里的_root和一些function
我想说在iterator中多存了一个const BSTree* _tree
却会产生以下的error message:
adtTest.h:127: instantiated from here
../../include/bst.h:55: 错误: 将 「const BSTree<AdtTestObj>」 做为 「
BSTree<T>::iterator BSTree<T>::findnext(BSTreeNode<T>*) [with T = AdtTestObj]
」 的 「this」 引数时丢弃了类型限定
错误部分的code如下:
iterator& operator ++ (){
iterator li = (_tree->findnext(_node));
_node = li._node;
return (*this);
}
其中:
findnext是我在BSTree class中自行定义的private function:
iterator findnext(BSTreeNode<T>* p){
if(p == _tail){ iterator li(p,const_cast<BSTree<T>*>(this)); return li;}
if((p->_right) != 0){ return findmin(p->_right);}
else {
BSTreeNode<T>* q = (p->_parent);
while((q->_data) < (p->_data)){
q = q->_parent;
}
iterator li(q,const_cast<BSTree<T>*>(this)); //call constructor
return li;
}
}
我不太懂error message的意义@@,想了很久不知道怎麽解决...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.248.220
1F:推 keyboardle:我想是const的问题.看是要删const还是要加const 12/09 13:28
2F:→ keyboardle:还有应该是findnext的问题.不是++吧我想 12/09 13:34
3F:推 ric2k1:不晓得你的问一跟 2477 有没有关系? 12/09 15:43
4F:→ lleyfede:恩...我的写法跟2477几乎一样耶...还是看不出bug在哪@@ 12/09 16:05
5F:推 herbert570:把 findnext 设成 const吧! 12/09 16:33
6F:推 herbert570:member fxn implicitly 把 this 丢进去当它的 argument 12/09 16:37
7F:→ herbert570:所以当 member fxn 不是 const 的话,那麽 this 就不是 12/09 16:37
8F:→ herbert570:const 的 this 12/09 16:38
9F:→ lleyfede:谢谢修源!!总算complie过了! 12/09 17:28