开发者

Defining a struct with member structs

I am trying t开发者_StackOverflow社区o 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


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜