作者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