2014-02-27 102 views
1

的我遇到編譯錯誤,當我嘗試這樣做訂購結構

struct Child{ 
    char Name[8]; 
    Parent *P; 
}; 

struct Parent{ 
    char Name[8]; 
    Child *C; 
}; 

編譯器抱怨無法找到在struct Child家長,但他們交換順序沒有幫助。編譯器會抱怨它在Parent中找不到Child。我如何解決這個錯誤,爲什麼我寫程序時沒有突出顯示錯誤。

+0

嘿太晚了答案..但我要補充不要使用Parent!如果你在GUI應用程序,然後父通常保留字的窗口/應用程序的父... – Spektre

回答

6

您可以添加forward declaration

struct Parent; //forward declaration 
struct Child{ 
    char Name[8]; 
    Parent *P; 
}; 
+0

只是爲了完整性,請注意,你將無法做到這一點,如果P不是一個指向父,但如果它是Parent的一個實例。 –

+0

是的,也仍然無效*可以用來代替父母*或孩子* ... – Spektre

+0

非常感謝。你救了我!! –

1

您需要轉發聲明父,才能在結構客戶使用它:

struct Parent; 

struct Child{ 
    char Name[8]; 
    Parent *P; 
}; 

struct Parent{ 
    char Name[8]; 
    Child *C; 
};