簡體   English   中英

循環依賴

[英]Circular dependency

我有一個稱為Point的類,另一個包含樹類的名稱空間。 該樹是平衡的二進制搜索樹,並使用compare函數比較其關鍵字並將其放在正確的位置。 我的樹包含Point ,對於每個點我都必須知道它屬於哪個樹,因此我嘗試在Point類中添加一個指向樹的指針,如下所示:

class Point{
   public:
   double x, y;
   std::set<std::multiset<Point,Tree::compare()>*>s; //Tree is the name of the namespace
// some other data
}

我的問題是,由於我的樹使用Point.h (因為它存儲Point S)我不能添加LayerThree.h我的,所以我不能使用Tree::compare()在我的Point.h 我試圖添加一個新的文件,如cmp.h然后將我的compare函數放入其中,但這沒有幫助。 我該怎么辦?

編輯:我不能將我的樹和Point類放到同一文件中,因為還有其他文件需要包含Point.h

正如Jepessen所說,您需要向前聲明。 這是一個例子:

A類依賴於B類,而B類依賴於A類,使用如下的前向聲明:

在“ Bh”類中:

#include "A.h"

class B
{
   //Do stuff
};

在“ Ah”類中:

 // Forward declaration of class B, this tells the compiler to not worry about 
 // class B as somewhere later in compilation class B will be declared
class B;

class A
{
   //Do stuff
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM