Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

問題

0 views
Skip to first unread message

drifter

unread,
Mar 9, 2000, 3:00:00 AM3/9/00
to

請教大家一個問題about C++

這個程式共包函 "tree.h", "tree.cpp", "treeNode.h", "treeNode.cpp"以及
"main.c"五個檔案。以下簡單列出其內容。

("......"為和問題無關的不重要部份)

*************************************************
// content of tree.h

#include "treeNode.h"

class Tree
{
TreeNode *node;
........;
........;
........;
void treeFunction(); ------>(5)
};
**************************************************

*************************************************
// content of tree.cpp

# include tree.h

void Tree::treeFunction()
{
.......;
.......;
}
...........
...........

***************************************************

*************************************************
// content of treeNode.h
class Tree; ----------------->(1)
class TreeNode
{
Node *leftNode;
Node *rightNode;
Tree *tree; -------------->(2)
........
........
treeNodeFunction() ---------------->(3)

};
**************************************************

*************************************************
// content of treeNode.cpp

#include "treeNode.cpp"

void treeNode::treeNodeFunction()
{
tree->treeFunction(); ---------------(4)
}
************************************************

*************************************************
// condtent of main.c
#include TreeNode.h
#include Tree.h

main()
{
............
..........

}
****************************************************

先看treeNode.h這個檔案

我希望在每個TreeNode中, 可以存取其所屬的tree。故在(2)的地方加入

所屬Tree的指標。但是我沒有在這之前include tree.h, 而是只宣告Tree

是一個class而已(在(1)的地方)。 原因是在tree.h中, 我include了treeNode.h。

若我在treeNode.h 中include 了tree.h, 同時又在tree.h中include了treeNode.h

那就變成了先有雞還是先有蛋的問題了,在編譯時會出錯。

以上都沒有問題,但是到了treeNode.cpp中問題就發生了。


在(3)的地方顯示treeNodeFunction()為TreeNode的成員,故要在treeNode.cpp中定義。


當在(4)的地方,treeNodeFunction()中乎叫treeFunction()時卻有困難, 因為編譯

器卻不認識treeFunction()。原因是剛剛只有在treeNode.h中宣告Tree是一個class

而已,沒有告訴編譯器Tree中有一個成員叫做treeFunction();


以上就是我的問題,要怎麼解決呢?


// 謝謝大家耐心看完以上問題。


--
※ Origin: 台大機械 [140.112.14.4] ◆ From: gear3.me.ntu.edu.tw

0 new messages