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

标签 c visual-studio-2012 struct

我正在为一个类(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/

相关文章:

c++ - 在 C++ 中处理未终止的 char 数组有多容易?

c - 启用 HANDLE_PRAGMA_PACK_WITH_EXPANSION

c - 使用 execlp() 从字符串运行 shell 命令

c# - Windows Phone 8 模拟器未启动。错误代码 0x80131500

c++ - 无法从 .dat 文件读取数据(从 Simulink 创建的 VS2012 C++ 项目)

c# - 在 C# 中比较两个结构的值

c - 阅读文档并在该文档中查找特定单词

C 数组正在覆盖索引

json - 0x800a1391-JavaScript运行时错误: 'JSON' is undefined in IE 10

inheritance - 如何避免具有语义相同字段/属性的不同结构的代码重复?