gpt4 book ai didi

c - 结构中的结构导致问题

转载 作者:太空宇宙 更新时间:2023-11-04 03:50:06 25 4
gpt4 key购买 nike

我正在为一个类(class)设计一个基本的游戏引擎。有 3 个基本部分,游戏对象(我游戏中的对象)、事件(基于事件的引擎)和参数(可以是事件内部传递的整数和 float )。如果我不包含“Event.h”(其中包含 GameObject.h 和 Argument.h),我不会收到任何错误,因此它可以干净地编译。但是,如果我尝试包含头文件,它就会出错。我怎么看都看不出有什么问题,我的头文件没有循环依赖,我所有的结构都定义得很清楚。头文件不应该相互重新定义。不太确定现在该怎么办。我将在下面添加文件。

GameObject.h

#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H

typedef struct GameObject;
struct GameObject
{
char *name;
};

#endif

Argument.h

#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

事件.h

#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

我一遍又一遍地重复这些错误。其他错误基本上来 self 的结构未定义,因此编译器大喊他们的类型不存在。

//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;

struct Argument是基础类型,你想要 typedef它作为Argument .

现在,您基本上是在尝试告诉它替换单词 struct用这个词 Argument无论它发生在哪里,都只能导致眼泪。

通常的用法是这样的:

typedef struct GameObject
{
char *name;
} GameObject;

或:

struct GameObject
{
char *name;
};
typedef struct GameObject GameObject;

关于c - 结构中的结构导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21373263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com