Hello,
I'm working on a game, and for this game, I created a class. There is one object of this class for each player in the game:

class PlayerClass
{
    public:
    PlayerClass(Nationality);   // Constructor
    ~PlayerClass();             // Destructor
    sf::Sprite PlayerFeet;
    sf::Sprite Arms;
    sf::Sprite Body;
    sf::Sprite Head;
    int HitPoints;
    GameClass CurrentClass;
};
PlayerClass::PlayerClass(Nationality natl)
{
    PlayerFeet.SetImage(iFeet);
    HitPoints = 100;
    CurrentClass = SCOUT_SNIPER;
    if(natl == UK)
    {
        Arms.SetImage(iUK_Arms);
        Body.SetImage(iUK_Body);
        Head.SetImage(iUK_Head);
    }
    else
    {
        Arms.SetImage(iNZ_Arms);
        Body.SetImage(iNZ_Body);
        Head.SetImage(iNZ_Head);
    }
}
PlayerClass::~PlayerClass()
{
}

That is in the Classes.cpp file. If you can't tell, I'm using SFML, hence the sf:: Sprite's (added a space so it wouldn't make a :S ). These are the enumerations I have, in the Main.h header file:

enum Nationality { US, UK, RU, NZ, JP };
enum GameClass { SUPPORT, MEDIC, ENGINEER, MARKSMAN, OFFICER, SCOUT_SNIPER };

In the same project, I have the Main.cpp file:

PlayerClass PlayerA(UK);

(I have "extern PlayerClass PlayerA" in the Main.h file)
Now, when I compile this, it gives me an error, pointing to the line PlayerA is declared in Main.cpp:

"error: variable `PlayerClass PlayerA' has initializer but incomplete type"
"error: storage size of `PlayerA' isn't known"

What did I do to receive this error?

Recommended Answers

All 13 Replies

So I need to write the class declaration, method definitions, etc. in a header file, and include it in every other relevant file? If I do that, it basically says that the class has been declared multiple times (once for each file I include the header in).

What you want to do is have only the class definition in the header, and put the function implementations in yet another file (say, PlayerClass.cpp or something similar). You will need to put the file into your project or Makefile to ensure that it is linked into the executable.
Player.h

#ifndef PLAYER_H
#define PLAYER_H

#include "main.h"

class PlayerClass
{
public:
    PlayerClass(Nationality);   // Constructor
    ~PlayerClass();             // Destructor
    sf::Sprite PlayerFeet;
    sf::Sprite Arms;
    sf::Sprite Body;
    sf::Sprite Head;
    int HitPoints;
    GameClass CurrentClass;
};

#endif

Player.cpp

#include "main.h"
#include "Player.h"

PlayerClass::PlayerClass(Nationality natl)
{
    PlayerFeet.SetImage(iFeet);
    HitPoints = 100;
    CurrentClass = SCOUT_SNIPER;
    if(natl == UK)
    {
        Arms.SetImage(iUK_Arms);
        Body.SetImage(iUK_Body);
        Head.SetImage(iUK_Head);
    }
    else
    {
        Arms.SetImage(iNZ_Arms);
        Body.SetImage(iNZ_Body);
        Head.SetImage(iNZ_Head);
    }
}
PlayerClass::~PlayerClass()
{
}

Alright, I did as recommended, but now the other .cpp files don't recognize PlayerClass as a class. I did not #include PlayerClass.cpp or its header in any of the other .cpp files, as that would result in multiple declarations of PlayerClass.

If you #include the header, but not the implementation file, then the other files should recognize the class, without the multiple definition problem. See here for more details.

When I #include the header, it gives me an error on line 2497 of locale_facets.tcc, which to this point I have only determined that it is a multiple-declaration error.

Za? That's... bizarre. I'll have to see what I can find about that file.

In the meanwhile, can you post the whole, exact error message that you're getting, as well as the current version of the PlayerClass header?

This is the header file:

//-----------------------------------------------
/// This is a class header file for the class "PlayerClass"
//-----------------------------------------------

#ifndef PLAYERCLASS_H
#define PLAYERCLASS_H 1

#include "Battlefront.h"

class PlayerClass
{
    public:
    PlayerClass(Nationality);   // Constructor
    ~PlayerClass();             // Destructor
    sf::Sprite PlayerFeet;
    sf::Sprite Arms;
    sf::Sprite Body;
    sf::Sprite Head;
    int HitPoints;
    GameClass CurrentClass;
};

#endif

And this is the error:

obj\Debug\Battlefront.o||In function `ZSt17__verify_groupingPKcjRKSs':|
C:\Program Files (x86)\CodeBlocks\bin\..\lib\gcc\mingw32\3.4.4\..\..\..\..\include\c++\3.4.4\bits\locale_facets.tcc|2497|multiple definition of `MainWindow'|
obj\Debug\VariableSetUp.o:C:\Users\Justin\Documents\C++ Projects\Battlefront\VariableSetUp.cpp|29|first defined here|
obj\Debug\Battlefront.o||In function `Z41__static_initialization_and_destruction_0ii':|
C:\Users\Justin\Documents\C++ Projects\Battlefront\Battlefront.cpp|12|multiple definition of `Mode'|
obj\Debug\VariableSetUp.o:C:\Users\Justin\Documents\C++ Projects\Battlefront\VariableSetUp.cpp|37|first defined here|
||=== Build finished: 4 errors, 0 warnings ===|

This looks to have something to do with the Battlefront.h and Battlefront.cpp files, rather than with the code shown here. Specifically, it has to do with two variables (or possibly functions) named MainWindow and Mode . Could you show where these are declared?

Both are declared in Battlefront.cpp with externs in Battlefront.h. MainWindow is of type sf::RenderWindow (I'm using SFML), and Mode is of a special enumeration GameSection.

OK, and do either of these appear in VariableSetup.cpp ?

hi friend.........
In this post i want to ask a thing that how can i add more header file for better.
thanks
Nancy

Ah yes, they appear in VariableSetup.cpp. When I removed them, the error vanished. The only issue is that it immediately stops working once it runs, although that could be an error on my part.
Thank you so much!
P.S. Nancy, I suggest making your own thread, and not posting on other people's, if you have a question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.