Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:01:52+00:00 2026-06-05T20:01:52+00:00

I’ve recently been struggling with multiple file inclusion errors. I’m working on a space

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:01:57+00:00Added an answer on June 5, 2026 at 8:01 pm

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

    When you include cpp files, You end up breaking the One Definition Rule(ODR).
    Usually, Your header files(.h) will define the class/structures etc and your source(.cpp) files will define the member functions etc.
    As per ODR you can have only definition for each variable/function etc, including the same cpp file in multiple files creates more than one definition and hence breaks the ODR.

    How should you go about this?

    Note that to be able to create objects or call member functions etc, All you need to do is include the header file which defines that class in the source file which needs to create the object etc. You don’t need to include source files anywhere.

    What about Forward Declarations?

    It is always preferred to use forward declaring classes or structures instead of including the header files, doing so has significant advantages, like:

    • Reduced compilation time
    • No Pollution of global namespace.
    • No Potential clash of preprocessor names.
    • No Increase in Binary size(in some cases though not always)

    However, Once you forward declare a type you can only perform limited operations on it because the compiler sees it as an Incomplete Type. So You should try to Forward declarations always but you can’t do so always.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.