I recently struggled with multiple file inclusion errors. I am working on a space arcade game and have divided my classes / objects into different .cpp files and make sure that everything still works great together. I created the following header file:
#ifndef SPACEGAME_H_INCLUDED #define SPACEGAME_H_INCLUDED //Some Main constants #define PI 3.14159265 //Standard includes #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <math.h> #include <string.h> #include <iostream> #include <vector> using namespace std; //SDL headers #include "SDL.h" #include "SDL_opengl.h" #include "SDL_mixer.h" #include "SDL_image.h" //Classes and project files #include "Player.cpp" #include "planet.cpp" #include "Destructable.cpp" #include "PowerUp.cpp" #include "PowerUp_Speed.cpp" #endif // SPACEGAME_H_INCLUDED
And at the top of each of my files, I included (only) this header file, which contains all the .cpp files and includes the standard.
However, I have a Player / Ship class that gave me errors like overriding the Ship class . As a result, I discovered a workaround by including the preifcess #ifndef and #define commands in the class definition file:
#ifndef PLAYER_H #define PLAYER_H #include "SpaceGame.h" struct Bullet { float x, y; float velX, velY; bool isAlive; }; class Ship { Ship(float sX,float sY, int w, int h, float velocity, int cw, int ch) { up = false; down = false; left = false; right = false; angle = 0; .... #endif
In this workaround, I lost erorrs "class / structure redefinition", but this gave me strange errors in my PowerUp_Speed ββclass file, which requires the Ship class:
#include "SpaceGame.h" class PowerUp_Speed : public PowerUp { public: PowerUp_Speed() { texture = loadTexture("sprites/Planet1.png"); } void boostPlayer(Ship &ship) { ship.vel += 0.2f; } };
I get the following errors: " Invalid use of the incomplete type" struct Ship " and ' Forward declaration' struct ship ' '
I believe that the origin of these errors still causes problems with the inclusion of multiple files. I described each step I took to reduce the number of errors, but so far none of the messages I found on Google have helped me, so I politely ask if any of you can help me find the origin of the problems and fix it.