C 导致问题的结构中的结构
我正在为一个班级设计一个基本的游戏引擎。有3个基本部分,GameObjects(在我的游戏中是对象)、Events(基于事件的引擎)和Arguments(可以是在事件中传递的int和float)。如果我不包括“Event.h”(其中包括GameObject.h和Argument.h),我不会得到任何错误,因此它可以干净地编译。然而,如果我试图包含头文件,它会抛出一个合适的。无论我怎么努力,我都看不到任何问题,我的头文件没有循环依赖关系,我的所有结构都定义得很清楚。头文件不应该相互重新定义。我不知道现在该怎么办。我将在下面添加文件 GameObject.hC 导致问题的结构中的结构,c,visual-studio-2012,struct,C,Visual Studio 2012,Struct,我正在为一个班级设计一个基本的游戏引擎。有3个基本部分,GameObjects(在我的游戏中是对象)、Events(基于事件的引擎)和Arguments(可以是在事件中传递的int和float)。如果我不包括“Event.h”(其中包括GameObject.h和Argument.h),我不会得到任何错误,因此它可以干净地编译。然而,如果我试图包含头文件,它会抛出一个合适的。无论我怎么努力,我都看不到任何问题,我的头文件没有循环依赖关系,我的所有结构都定义得很清楚。头文件不应该相互重新定义。我不知
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
typedef struct GameObject;
struct GameObject
{
char *name;
};
#endif
#ifndef ARGUMENT_H
#define ARGUMENT_H
#include "GameObject.h"
enum ARG_TYPES
{
TYPE_INT = 0,
TYPE_FLOAT,
TYPE_DOUBLE,
TYPE_STRING,
TYPE_CHAR,
TYPE_GO,
TYPE_NULL = -1
};
typedef struct Argument;
struct Argument
{
char *name;
int type;
union
{
int _int;
float _float;
double _double;
char *_string;
char _char;
GameObject *_go;
};
};
#endif
#ifndef EVENT_H
#define EVENT_H
#include "GameObject.h"
#include "Argument.h"
#include "stdlib.h"
#define MAX_ARGS 8
enum EVENT_TYPE
{
EVENT_INPUT = 1,
EVENT_GAMEPLAY = 2,
EVENT_COLLISION = 3,
EVENT_OBJECT = 4,
EVENT_NULL = -1
};
typedef struct Event;
struct Event
{
int type; //this is the type of event that this event is.
char *name; //the name of the current event. If we include hashing, this will change to a number
unsigned int arg_num; //the number of arguments currently held by the event. This is mostly for adding events
Argument *args; //An array of arguments. To understand an argument, look at Argument.h
int flag; //A flag as to whether this event is in use. Used for optimizing searching
};
//there are a bunch of functions here, but they just cause the errors.
#endif
参数.h
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
typedef struct GameObject;
struct GameObject
{
char *name;
};
#endif
#ifndef ARGUMENT_H
#define ARGUMENT_H
#include "GameObject.h"
enum ARG_TYPES
{
TYPE_INT = 0,
TYPE_FLOAT,
TYPE_DOUBLE,
TYPE_STRING,
TYPE_CHAR,
TYPE_GO,
TYPE_NULL = -1
};
typedef struct Argument;
struct Argument
{
char *name;
int type;
union
{
int _int;
float _float;
double _double;
char *_string;
char _char;
GameObject *_go;
};
};
#endif
#ifndef EVENT_H
#define EVENT_H
#include "GameObject.h"
#include "Argument.h"
#include "stdlib.h"
#define MAX_ARGS 8
enum EVENT_TYPE
{
EVENT_INPUT = 1,
EVENT_GAMEPLAY = 2,
EVENT_COLLISION = 3,
EVENT_OBJECT = 4,
EVENT_NULL = -1
};
typedef struct Event;
struct Event
{
int type; //this is the type of event that this event is.
char *name; //the name of the current event. If we include hashing, this will change to a number
unsigned int arg_num; //the number of arguments currently held by the event. This is mostly for adding events
Argument *args; //An array of arguments. To understand an argument, look at Argument.h
int flag; //A flag as to whether this event is in use. Used for optimizing searching
};
//there are a bunch of functions here, but they just cause the errors.
#endif
事件.h
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
typedef struct GameObject;
struct GameObject
{
char *name;
};
#endif
#ifndef ARGUMENT_H
#define ARGUMENT_H
#include "GameObject.h"
enum ARG_TYPES
{
TYPE_INT = 0,
TYPE_FLOAT,
TYPE_DOUBLE,
TYPE_STRING,
TYPE_CHAR,
TYPE_GO,
TYPE_NULL = -1
};
typedef struct Argument;
struct Argument
{
char *name;
int type;
union
{
int _int;
float _float;
double _double;
char *_string;
char _char;
GameObject *_go;
};
};
#endif
#ifndef EVENT_H
#define EVENT_H
#include "GameObject.h"
#include "Argument.h"
#include "stdlib.h"
#define MAX_ARGS 8
enum EVENT_TYPE
{
EVENT_INPUT = 1,
EVENT_GAMEPLAY = 2,
EVENT_COLLISION = 3,
EVENT_OBJECT = 4,
EVENT_NULL = -1
};
typedef struct Event;
struct Event
{
int type; //this is the type of event that this event is.
char *name; //the name of the current event. If we include hashing, this will change to a number
unsigned int arg_num; //the number of arguments currently held by the event. This is mostly for adding events
Argument *args; //An array of arguments. To understand an argument, look at Argument.h
int flag; //A flag as to whether this event is in use. Used for optimizing searching
};
//there are a bunch of functions here, but they just cause the errors.
#endif
我一遍又一遍地重复这些错误。其他错误基本上来自未定义的结构,因此编译器会大喊它们的类型如何不存在
//this one is repeated over and over a TON.
error C2143: syntax error : missing ')' before '*'
error C2143: syntax error : missing '{' before '*'
error C2059: syntax error : 'type'
error C2059: syntax error : ')'
我正在使用Visual Studio 2012 Professional,用C语言进行编译(我手动设置了编译器选项)。您使用
typedef
做了一些非常奇怪的事情
你在这里写的是:
typedef struct Argument;
应该是这样的:
typedef struct Argument Argument;
typedef struct GameObject
{
char *name;
} GameObject;
struct-Argument
是基础类型,您希望typedef
将其设置为Argument
现在,您基本上是想告诉它,无论在哪里出现,都要用单词参数
替换单词结构
,这只会导致流泪
通常的用法如下:
typedef struct Argument Argument;
typedef struct GameObject
{
char *name;
} GameObject;
或:
在结构中使用该参数之前,我试图确保该参数是100%定义的。我试着按照你的建议去做,没有任何改进。以前,我的所有结构都被定义为
typedef struct{}参数代码>对格式设置感到抱歉,我反对这个评论框。这几乎肯定是你的问题。另一方面,使用诸如EVENT_H
和ARGUMENT_H
等短而常见的名称作为include-guard是有风险的,因为越短越常见,包含的其他文件也会定义它的可能性越高。像DAVES_NEW_GAME_ENGINE_EVENT_H
之类的东西更安全。上面另一个人说的话解决了这个问题,使用int和float会导致大量问题。在retrospect中可能是一种愚蠢的命名方式,因为_变量是为Microsoft stuff保留的。您应该将以\u
开头的名称视为为为实现保留的名称。实现可能使用诸如\u int
之类的名称。