2014-02-27 103 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; 
};