作者trancewu (exile me)
看板EE_DSnP
標題[問題] invalid use of nonstatic data member _root
時間Tue May 20 18:50:36 2008
> 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
1F:推 ric2k1:你在 iterator 的 constructor 裡試著使用 _root? 05/20 19:13
2F:→ ric2k1:他應該是看不到 _root, 除非他看得到 BSTree 這個 object.. 05/20 19:14
3F:→ trancewu:yes,I declared iterator as a friend of BSTree 05/20 19:14
4F:推 ric2k1:friend 只是讓 BSTree 可以使用 iterator 的 private 05/20 19:16
5F:→ ric2k1:members 而不用透過 public methods, 他並不能改變 data 05/20 19:16
6F:→ ric2k1:member 的 access 必須經過實體的 object... 05/20 19:17
7F:→ ric2k1:除非是 static data member, 才可以用 ClassName::_data... 05/20 19:17
8F:→ ric2k1:這樣沒有透過物件的存取. 05/20 19:18
9F:推 ric2k1:不過 _root 應該是不能 declare 成 static. 否則所有的 05/20 19:19
10F:→ trancewu:不能在BSTree裡這樣宣告嗎? 05/20 19:19
11F:→ trancewu: friend class BSTree<T>::iterator; 05/20 19:19
12F:→ ric2k1:BST 都會看到同樣的一個 _root... 05/20 19:19
13F:推 ric2k1:可以, 但是重點是 _root 不是 static data member... 05/20 19:20
14F:推 ric2k1:除非你將 BSTree& 傳給 iterator(), 否則 iterator() 怎麼 05/20 19:21
15F:→ ric2k1:可以看得到 _root? 05/20 19:21
16F:推 ric2k1:不過將 BSTree& 傳給 iterator() 很奇怪 <-- you can't do 05/20 19:22
17F:→ ric2k1:this either. 05/20 19:22
18F:推 ric2k1:昨天 BattleRoyale 有問我一個類似的問題, 請他來現身說法 05/20 19:24
19F:推 ric2k1:一下? 05/20 19:24
20F:推 battlecruise:我有一個問題想請教 就是我iterator裡 想用BSTree的 05/20 19:27
21F:→ battlecruise:function 請問該怎麼作呢? 在BSTree裡宣告friend嗎? 05/20 19:27