I've recently been struggling with multiple file inclusion errors. I'm working on a space arcade game and have divided my classes/objects into different .cpp files and to make sure everything still works fine together I have build 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 every one of my files I included (only) this header file which holds all .cpp files and the standard includes.
However, I have a Player/Ship class that has given me errors of the 'redefinition of Ship class' type. I eventually found a workaround by including preprocessor #ifndef and #define commands in the class definition file:
#ifndef PLAYER_H
#define PLAYER_H
/** Player class that controls the flying object used for the space game */
#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
With this workaround I lost the 'class/struct redefinition' erorrs but it gave me weird errors in my class file PowerUp_Speed that 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've been getting the following errors: 'Invalid use of incomplete type 'struct Ship'' and 'Forward declaration of 'struct ship''
I believe the origin of these errors is still in my trouble with the multiple file inclusion errors. I described every step I took in order to reduce my amount of errors but so far none of all the posts I've found on Google helped me so that's why I'm politely asking if any of you could help me find the origin of the problems and a fix.