我試圖實現具有兩個類的樹狀結構:樹和節點。問題在於,我想從每個類中調用另一個類的函數,所以只需簡單的前向聲明是不夠的。讓我們來看一個例子:頭文件之間的循環依賴關係
tree.h中:
#ifndef TREE_20100118
#define TREE_20100118
#include <vector>
#include "Node.h"
class Tree
{
int counter_;
std::vector<Node> nodes_;
public:
Tree() : counter_(0) {}
void start() {
for (int i=0; i<3; ++i) {
Node node(this, i);
this->nodes_.push_back(node);
}
nodes_[0].hi(); // calling a function of Node
}
void incCnt() {
++counter_;
}
void decCnt() {
--counter_;
}
};
#endif /* TREE_20100118 */
Node.h:
#ifndef NODE_20100118
#define NODE_20100118
#include <iostream>
//#include "Tree.h"
class Tree; // compile error without this
class Node
{
Tree * tree_;
int id_;
public:
Node(Tree * tree, int id) : tree_(tree), id_(id)
{
// tree_->incCnt(); // trying to call a function of Tree
}
~Node() {
// tree_->decCnt(); // problem here and in the constructor
}
void hi() {
std::cout << "hi (" << id_ << ")" << endl;
}
};
#endif /* NODE_20100118 */
調用樹:
#include "Tree.h"
...
Tree t;
t.start();
這只是一個簡單的例子來說明問題。所以我想要的是從Node對象調用Tree的函數。
更新#1:感謝您的答案。我試圖解決像Java一樣的問題,即每個類只使用一個文件。看來我將不得不開始分離.cpp和.h文件...
更新#2:下面,按照提示,我也粘貼完整的解決方案。謝謝,問題解決了。