本文介绍了类型定义/结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 不知怎的,我完全忘记了typedef / structs是如何工作的。 为什么这不起作用: typedef struct _MY_STRUCT { int a; int b; struct _MY_STRUCT * c; } MY_STRUCT; 我的编译器抱怨不完整的结构MY_STRUCT等等 如果我尝试使用MY_STRUCT。 copx 解决方案 其次,请尝试这种方式: typedef struct MyStruct MyStruct; struct MyStruct { int a; int b; MyStruct * c; }; 请注意,结构标记名称和typedef别名可以是相同的,因为 他们在不同的名称空间。 - - 马克 - > - ,具有前导_的名称保留(一般)用于实现所以避免它们。 带有前导__的IIRC名称保留,一个_完美好的IMO。 其次,请尝试这种方式: typedef struct MyStruct MyStruct; struct MyStruct { int a; int b; MyStruct * c; }; 请注意结构标签名称和typedef别名可以是相同的,因为它们位于不同的名称空间中。 好吧,它的确有效。但我真的不喜欢 这个类型和结构具有相同名称的事实。 如果他们不这样做就会破坏。 这当然是品味/风格的问题,但我仍然希望听到一个选项来实现这个工作 使用不同命名的struct /类型。 copx 它真的应该,模数UB。 And它确实。对不起,我没有仔细阅读 编译器警告。 某处我做了类似的事情: struct MY_STRUCT x; 这导致了不完整的结构麻烦。 copx(惭愧) Somehow I totally forgot how typedefs/structs work.Why doesn''t this work: typedef struct _MY_STRUCT {int a;int b;struct _MY_STRUCT *c;} MY_STRUCT; My compiler complains about "incomplete structure MY_STRUCT" etc.If I try to use MY_STRUCT. copx 解决方案 Second, try it this way: typedef struct MyStruct MyStruct; struct MyStruct{int a;int b;MyStruct *c;}; Note that a struct tag name and the typedef alias can be the same sincethey are in different namespaces. --- Mark ->-- First, names with leading _ are reserved (in general) for the implemtation so avoid them. IIRC names with leading __ are reserved, one _ is perfectly ok IMO. Second, try it this way: typedef struct MyStruct MyStruct; struct MyStruct { int a; int b; MyStruct *c; }; Note that a struct tag name and the typedef alias can be the same since they are in different namespaces. Well, it works thanks. But I really don''t like the factthat the type and the struct have the same name.And it breaks if they don''t.It''s a matter of taste/style of course but I would stilllike to hear about an option to get this workingwith differently named struct/type. copx It really should, modulo UB. And it does. Sorry, I didn''t readthe compiler warning carefully enough.Somewhere I did something like: struct MY_STRUCT x; That ''caused the "incomplete struct" trouble. copx (ashamed) 这篇关于类型定义/结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 10:46