cehnehdeh
cehnehdeh

Reputation: 527

Structs within structs causing problems

I'm designing a basic game engine for a class. There are 3 basic parts, GameObjects (which are objects in my game), Events (Event based engine), and Arguments (Can be things such as ints and floats that are passed inside Events). If I don't include "Event.h" (which includes GameObject.h and Argument.h), I get no errors, so it compiles cleanly. However, if I try to include the header file, it throws a fit. I can't see any problems no matter how hard I look, my header files do not have a circular dependency, and all of my structs are clearly defined. Header files shouldn't be re-defining each other. Not really sure what to do now. I'll add the files below.

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

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

I get these errors repeated over and over. Other errors basically come from my structs not being defined, so the compiler yells about how their type doesn't exist.

//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 : ')'

I'm using Visual Studio 2012 Professional, compiling in C (I set the compiler option manually).

Upvotes: 1

Views: 56

Answers (1)

Crowman
Crowman

Reputation: 25908

You're doing something really weird with typedef.

What you've written here:

typedef struct Argument;

should be something like:

typedef struct Argument Argument;

struct Argument is the underlying type, and you want to typedef it as Argument.

Right now, you're basically trying to tell it to replace the word struct with the word Argument wherever it occurs, which can only lead to tears.

The usual usage would be something like:

typedef struct GameObject
{
  char *name;
} GameObject;

or:

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

Upvotes: 1

Related Questions