看板 EE_DSnP 關於我們 聯絡資訊
> compiling: adtTest.cpp ../../include/bst.h: In constructor `BSTree<T>::iterator::iterator(BSTreeNode<T>*) [with T = AdtTestObj]': ../../include/bst.h:106: instantiated from `size_t BSTree<T>::size() const [with T = AdtTestObj]' adtTest.h:97: instantiated from here ../../include/bst.h:57: error: invalid use of nonstatic data member 'BSTree<AdtTestObj>::_root' make[1]: *** [adtTest.o] Error 1 make: *** [all] Error 2 在make b時出現的問題, 完全不知道原因是什麼@@ 97行是end() 106行是size(), 不過這些都不是static member function... 第57行是iterator constructor, 會用到_root。 先謝謝大家了! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.251.153
ric2k1:你在 iterator 的 constructor 裡試著使用 _root? 05/20 19:13
ric2k1:他應該是看不到 _root, 除非他看得到 BSTree 這個 object.. 05/20 19:14
trancewu:yes,I declared iterator as a friend of BSTree 05/20 19:14
ric2k1:friend 只是讓 BSTree 可以使用 iterator 的 private 05/20 19:16
ric2k1:members 而不用透過 public methods, 他並不能改變 data 05/20 19:16
ric2k1:member 的 access 必須經過實體的 object... 05/20 19:17
ric2k1:除非是 static data member, 才可以用 ClassName::_data... 05/20 19:17
ric2k1:這樣沒有透過物件的存取. 05/20 19:18
ric2k1:不過 _root 應該是不能 declare 成 static. 否則所有的 05/20 19:19
trancewu:不能在BSTree裡這樣宣告嗎? 05/20 19:19
trancewu: friend class BSTree<T>::iterator; 05/20 19:19
ric2k1:BST 都會看到同樣的一個 _root... 05/20 19:19
ric2k1:可以, 但是重點是 _root 不是 static data member... 05/20 19:20
ric2k1:除非你將 BSTree& 傳給 iterator(), 否則 iterator() 怎麼 05/20 19:21
ric2k1:可以看得到 _root? 05/20 19:21
ric2k1:不過將 BSTree& 傳給 iterator() 很奇怪 <-- you can't do 05/20 19:22
ric2k1:this either. 05/20 19:22
ric2k1:昨天 BattleRoyale 有問我一個類似的問題, 請他來現身說法 05/20 19:24
ric2k1:一下? 05/20 19:24
battlecruise:我有一個問題想請教 就是我iterator裡 想用BSTree的 05/20 19:27
battlecruise:function 請問該怎麼作呢? 在BSTree裡宣告friend嗎? 05/20 19:27