0

I have two classes Object and Chassis and I include each class with the opposite ones header. This is causing a redefinition error. I understand why, but I don't know how to fix it. Here is the Code:

#ifndef GAME_h
#define GAME_h


#include "Object.h"

class Game{
     void AddObject(Object o);
     SDL_Renderer *GetRenderer(){ /*returns renderer*/ }
     /*random code*/
}

#endif

#ifndef OBJECT_h
#define OBJECT_h


#include "Game.h"

class Object{
     void CreateObject(/*settings*/);
     void SetTexture(Game g, const char* loc); //Uses Game to Load Texture
     /*random code*/
}

#endif

Thanks in advance!

Yami OG
  • 67
  • 1
  • 7

1 Answers1

0

You can declare a class first without a definition: The syntax would look like this: class Classname;

Make sure the declaration of this certain class will later be defined somewhere.

You can use the Classname class in the following part of the .h / .hpp file but you can not access its attributes and methods since they will be defined later.

Consider putting the method bodies, which need the attributes and methods of the other class, into a .cpp file.

For more details: https://en.cppreference.com/w/cpp/language/class

nerotyc
  • 16
  • 3