1,105,312 Community Members

Trouble with Classes

Member Avatar
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
vijayan121
Posting Virtuoso
1,769 posts since Dec 2006
Reputation Points: 1,097 [?]
Q&As Helped to Solve: 329 [?]
Skill Endorsements: 16 [?]
 
0
 

Put the class definition in a header file of its own and include that header in other translation units.

See: http://www.informit.com/articles/article.aspx?p=26039

Member Avatar
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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).

Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,384 posts since Oct 2010
Reputation Points: 635 [?]
Q&As Helped to Solve: 225 [?]
Skill Endorsements: 28 [?]
 
0
 

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()
{
}
Member Avatar
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,384 posts since Oct 2010
Reputation Points: 635 [?]
Q&As Helped to Solve: 225 [?]
Skill Endorsements: 28 [?]
 
0
 

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.

Member Avatar
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,384 posts since Oct 2010
Reputation Points: 635 [?]
Q&As Helped to Solve: 225 [?]
Skill Endorsements: 28 [?]
 
0
 

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?

Member Avatar
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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 ===|
Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,384 posts since Oct 2010
Reputation Points: 635 [?]
Q&As Helped to Solve: 225 [?]
Skill Endorsements: 28 [?]
 
0
 

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?

Member Avatar
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,384 posts since Oct 2010
Reputation Points: 635 [?]
Q&As Helped to Solve: 225 [?]
Skill Endorsements: 28 [?]
 
0
 

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

Member Avatar
seo_charlotte
Newbie Poster
1 post since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: