使用成员结构定义结构
我试图定义一个包含三个成员结构(在其他标头中定义)的 Sprite 结构。由于某种原因,编译器给出外部结构的错误:
C2061 错误:标识符 'GraphicsComponent'
C2061 错误:标识符 'PhysicsComponent'
AFObjectFactory.h http://pastebin.com/raw.php?i=ps9DvgQG
AFObjectFactory.c http://pastebin.com/raw.php?i=HTEmSkdu
AFGraphics.h http://pastebin.com/raw.php?i=GpsXfy1n
AFGraphics.c http://pastebin.com/raw.php?i=cm3s6Nqg
AFPhysics.h http://pastebin.com/raw.php?i=DmQKBxpW
AFPhysics.c http://pastebin.com/raw.php?i=tsVQVUCC
#ifndef AFOBJECTFACTORY_H
#define AFOBJECTFACTORY_H
struct GameObjectComponent_t
{
struct GameObjectComponent_t* next;
};
typedef struct GameObjectComponent_t GameObjectComponent;
struct Sprite_t
{
GameObjectComponent GameObjectProperties;
GraphicsComponent GraphicsProperties;
PhysicsComponent PhysicsProperties;
};
typedef struct Sprite_t Sprite;
Sprite* CreateSprite(int color,
int screenX, int screenY, int worldX, int worldY,
int h, int w);
#endif
I am trying to define a Sprite structure that contains three member structures (defined in other headers). For some reason, the compiler gives errors for the outside structs:
C2061 error: identifier 'GraphicsComponent'
C2061 error: identifier 'PhysicsComponent'
AFObjectFactory.h
http://pastebin.com/raw.php?i=ps9DvgQG
AFObjectFactory.c
http://pastebin.com/raw.php?i=HTEmSkdu
AFGraphics.h
http://pastebin.com/raw.php?i=GpsXfy1n
AFGraphics.c
http://pastebin.com/raw.php?i=cm3s6Nqg
AFPhysics.h
http://pastebin.com/raw.php?i=DmQKBxpW
AFPhysics.c
http://pastebin.com/raw.php?i=tsVQVUCC
#ifndef AFOBJECTFACTORY_H
#define AFOBJECTFACTORY_H
struct GameObjectComponent_t
{
struct GameObjectComponent_t* next;
};
typedef struct GameObjectComponent_t GameObjectComponent;
struct Sprite_t
{
GameObjectComponent GameObjectProperties;
GraphicsComponent GraphicsProperties;
PhysicsComponent PhysicsProperties;
};
typedef struct Sprite_t Sprite;
Sprite* CreateSprite(int color,
int screenX, int screenY, int worldX, int worldY,
int h, int w);
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码不包含其他标头,这些标头应该包含
GraphicsComponent
和PhysicsComponent
的定义。 添加类似的内容您应该在上面的结构定义之前
。更新
您的标头之间存在循环依赖关系:AFGraphics.h 和 AFPhysics.h
#include
AFObjectFactory.h。您必须消除这种循环依赖性才能编译代码。最直接的方法是将 AFObjectFactory.h 中的 #includes 替换为其他结构的前向声明:
但更好的长期方法是在标头之间移动内容,以整理出需要定义它们的正确顺序,以及标题
#include
d。例如,现在 AFPhysics.h 包含
PhysicsComponent
的定义,以及一些带有Sprite
参数的方法。这使得编译器无法在没有前向声明的情况下解析标头,因为 AFObjectFactory.h 和 AFPhysics.h 相互依赖。如果您将带有
Sprite
参数的方法声明移至 AFObjectFactory.h 中,则 AFPhysics.h 对 AFObjectFactory.h 的依赖关系将不再存在,因此您可以删除#include "AFObjectFactory.h" AFPhysics.h 中的
行,而不是 AFObjectFactory.h 中的#include AFPhysics.h
,从而无需转发声明那里有structPhysicsComponent_t
。 (当然,其他安排也是可能的 - 这只是我想到的最简单的安排。重点是将您的定义分组在标头中,以便始终存在定义良好的包含顺序,而无需循环依赖。)Your code doesn't include the other headers, which supposedly contain the definitions of
GraphicsComponent
andPhysicsComponent
. You should add something likebefore the struct definitions above.
Update
There is a cyclic dependency between your headers: both AFGraphics.h and AFPhysics.h
#include
AFObjectFactory.h. You must eliminate this cyclic dependency in order for the code to compile.The most straightforward way is to replacee the #includes in AFObjectFactory.h with forward declarations of your other structs:
But the better long term approach would be to move stuff around between the headers to sort out the proper order in which they need to be defined, and the headers
#include
d.E.g. right now AFPhysics.h contains the definition of
PhysicsComponent
, and some methods withSprite
parameters. This makes it impossible for the compiler to parse the headers without a forward declaration, as AFObjectFactory.h and AFPhysics.h mutually depend on each other.If you moved the method declarations with
Sprite
parameters into AFObjectFactory.h, the dependency of AFPhysics.h on AFObjectFactory.h ceased to exist, thus you could remove the#include "AFObjectFactory.h"
line from AFPhysics.h and instead#include AFPhysics.h
in AFObjectFactory.h, eliminating the need to forward declarestruct PhysicsComponent_t
there. (Of course, other arrangements are possible too - this is just the simplest which comes to my mind. The point is to group your definitions in headers so that there is always a well defined include order, without cyclic dependencies.)