使用成员结构定义结构

发布于 2024-11-08 22:33:20 字数 1476 浏览 0 评论 0原文

我试图定义一个包含三个成员结构(在其他标头中定义)的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-11-15 22:33:20

您的代码不包含其他标头,这些标头应该包含 GraphicsComponentPhysicsComponent 的定义。 添加类似的内容

#include "otherHeader.h"

您应该在上面的结构定义之前

。更新

您的标头之间存在循环依赖关系:AFGraphics.h 和 AFPhysics.h #include AFObjectFactory.h。您必须消除这种循环依赖性才能编译代码。

最直接的方法是将 AFObjectFactory.h 中的 #includes 替换为其他结构的前向声明:

struct GraphicsComponent_t;
struct PhysicsComponent_t;

struct GameObjectComponent_t
{   
  struct GameObjectComponent_t* next;
};
typedef struct GameObjectComponent_t GameObjectComponent;

struct Sprite_t
{   
  GameObjectComponent GameObjectProperties;
  struct GraphicsComponent_t GraphicsProperties;
  struct PhysicsComponent_t PhysicsProperties;
};

但更好的长期方法是在标头之间移动内容,以整理出需要定义它们的正确顺序,以及标题#included。

例如,现在 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 and PhysicsComponent. You should add something like

#include "otherHeader.h"

before 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:

struct GraphicsComponent_t;
struct PhysicsComponent_t;

struct GameObjectComponent_t
{   
  struct GameObjectComponent_t* next;
};
typedef struct GameObjectComponent_t GameObjectComponent;

struct Sprite_t
{   
  GameObjectComponent GameObjectProperties;
  struct GraphicsComponent_t GraphicsProperties;
  struct PhysicsComponent_t PhysicsProperties;
};

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 #included.

E.g. right now AFPhysics.h contains the definition of PhysicsComponent, and some methods with Sprite 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 declare struct 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文