2016-04-28 68 views
1

我有兩個需要相互引用的結構,但是無論我先放哪一個都會拋出錯誤,因爲我使用的是未識別的類型。相互引用的結構

typedef struct _BLOCK 
{ 
    int size; 
    int offset; 
    struct _BLOCK *nextBlock; 
    struct _BLOCK *prevBlock; 
    Pool* parent; //here 
} Block; 

typedef struct _POOL 
{ 
    int size; 
    void* memory; 
    Block* Allocated; 
    Block* Unallocated; 
} Pool; 

有什麼辦法可以解決這個問題?

+2

以'_'開頭的名稱後面跟着一個大寫字母或另一個下劃線爲實現保留。 **不要使用它們**。 – Olaf

回答

4

您可以使用前向聲明。

typedef struct _POOL Pool; 

typedef struct _BLOCK 
{ 
    int size; 
    int offset; 
    struct _BLOCK *nextBlock; 
    struct _BLOCK *prevBlock; 
    Pool* parent; 
} Block; 

struct _POOL 
{ 
    int size; 
    void* memory; 
    Block* Allocated; 
    Block* Unallocated; 
}; 
+0

非常有幫助,歡呼聲 – David