C 中带有前向声明的 typedef Stuct
有类似的内容
typedef struct Data DATA, *DATA_PTR;
typedef struct Units UNITS, *UNITS_PTR;
struct Data
{
double miscData;
UNITS units;
};
struct Units
{
double x[2];
double y[2];
double z[2];
};
我的 project_typedef.h
文件中
。在另一个文件中,我有类似的内容:
void fileInput(DATA_PTR data)
{
//usual declarations and other things
data->miscData = 0; //Works!
data->units.x[0] = 5; //Doesn't work
//etc...
}
但是,这不起作用,因为单位是在 project_typedef.h
中的数据之后声明的(如果我切换它的工作顺序)。我得到的错误是“'.x'的左侧必须具有结构/联合类型”。我认为前瞻性声明可以解决这个问题。为什么不呢?
I have something like:
typedef struct Data DATA, *DATA_PTR;
typedef struct Units UNITS, *UNITS_PTR;
struct Data
{
double miscData;
UNITS units;
};
struct Units
{
double x[2];
double y[2];
double z[2];
};
in my project_typedef.h
file.
In another file, I have something like:
void fileInput(DATA_PTR data)
{
//usual declarations and other things
data->miscData = 0; //Works!
data->units.x[0] = 5; //Doesn't work
//etc...
}
However, this doesn't work since units is declared after data in project_typedef.h
(if I switch the order it works). The error that i get is "left of '.x' must have struct/union type". I thought that the forward declaration would fix this issue. Why not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您定义
Data
时,所有成员都必须是完整类型。由于UNITS
此时还不是完整的类型,因此这是行不通的。 (相比之下,UNITS_PTR
就可以了,因为指向不完整类型的指针是完整类型。)只需将
Units
定义放在之上数据
定义,你应该没问题。(正如 @cnicutar 已经指出的那样,您也错误地使用了数组
x
。)When you define
Data
, all members must be complete types. SinceUNITS
isn't a complete type at that point, this doesn't work. (By contrast,UNITS_PTR
would be fine, since pointers to incomplete types are complete types.)Simply put the
Units
definition above theData
definition and you should be fine.(As @cnicutar already noted, you're also using the array
x
wrong.)前向声明允许您在允许不完整类型的上下文中使用其名称。声明结构成员不是此类情况之一,必须了解完整的定义,因为它有助于结构布局。
The forward declaration allows you to use its name in context where an incomplete type is allowed. Declaring a struct member is not one of such cases, the complete definition must be known as it contributes to the struct layout.
对于结构体定义,您应该始终对结构体中的所有成员使用完整类型...但是
struct Data
中的UNITS 单位
的情况并非如此,它声明了一个变量名为units
的struct Units
类型从未在 struct Data 之前声明...这反映了一个错误。您应该放置Units
定义上面的Data
定义..一切都会正常工作..关于前向声明,这不起作用,因为每当定义结构变量时,编译器首先分配结构所需的内存(结构成员没有分配给它们的内存,除非它们链接到结构类型的变量..这就是为什么结构变量不能在结构模板内初始化)..:)
for a struct definition you should always use complete types for all members in a structure... but this is not the case with
UNITS units
instruct Data
,which declares a variable namedunits
of typestruct Units
which is never declared before the struct Data... this reflects an error.. you should place theUnits
definition aboveData
definition.. and all will work fine..and regarding forward declaration this does not work since whenever a struct variable is defined, the compiler first allocates the memory required to the struct (struct members donot have a memory allocated to them, unless they are linked to a struct type of variable.. thats why struct variables cant be initialized inside struct template).. :)
结构没有原型。这是因为编译器在使用它之前需要知道结构的大小。您可以在结构上使用指针,因为无论指针指向哪种类型,它们都具有已知的大小。
There is no prototype for struct. This is because compiler needs to know size of struct before using it. You could use pointer on struct, because pointers have known size no matter which type they point to.