我想在头文件中的不同结构定义中声明结构值。所以我在头文件中完成了以下内容。但是编译器给了我在A.H头文件中的错误信息field myStructMember has incomplete type
无法在c中的结构中声明结构成员
:
struct My_A{
int value;
};
在B.h头文件:
struct My_A; // forward declaration
struct My_B
{
struct My_A myStructMember; // error is here!
int differentValue;
};
是否有可能一个结构声明中声明结构成员?
编辑: My_A和My_B在不同的头文件中声明。
编辑2: 当我在B.h中添加“A.h”时,它就起作用了。它有任何问题吗? 在B.h头文件:
include "A.h" // added this line insead of forward declaration
struct My_B
{
struct My_A myStructMember; // not any error anymore
int differentValue;
};
@BLUEPIXY请参阅我的编辑。他们在不同的头文件中。 – Fer
是的,那不会工作。但是,您可以使'myStructMember'动态(一个指针)并管理.c文件代码中的适当分配。 – WhozCraig
@WhozCraig看到我的第二个编辑。我可以使用这种方法吗? – Fer