Multiple inclusion error, cannot find a solution

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 /** 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 

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.

+4
source share
1 answer

Usually you do not include cpp files.
You only need to include header files!

When you include cpp files, you end up breaking the One Definition Rule (ODR) .
Usually your header files (.h) will define class / structures, etc., And source files (.cpp) will define member functions, etc.
According to ODR, you can only have a definition for each variable / function, etc., Including the same cpp file in several files, creates several definitions and, therefore, violates ODR.

How do you do this?

Note that to create objects or calls to member functions, etc. All you have to do is include a header file that defines this class in the source file that the object should create, etc. You do not need to include source files anywhere.

What about leading ads?

It is always recommended to use forward declarations of classes or structures instead of including header files, therefore it has significant advantages, for example:

  • Reduced compilation time
  • No pollution to the global namespace.
  • There is no potential collision of preprocessor names.
  • No increase in binary size (in some cases, although not always)

However, as soon as you redirect the type declaration, you can only perform limited operations, because the compiler sees it as an Incomplete type . Therefore, you should always try to forward the ads, but you cannot always do this .

+9
source

All Articles