We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,408 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trouble with Classes

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?

4
Contributors
13
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
14
Views
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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

vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11

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

Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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()
{
}
Schol-R-LEA
Veteran Poster
1,042 posts since Oct 2010
Reputation Points: 452
Solved Threads: 167
Skill Endorsements: 11

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.

Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

Schol-R-LEA
Veteran Poster
1,042 posts since Oct 2010
Reputation Points: 452
Solved Threads: 167
Skill Endorsements: 11

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.

Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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?

Schol-R-LEA
Veteran Poster
1,042 posts since Oct 2010
Reputation Points: 452
Solved Threads: 167
Skill Endorsements: 11

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 ===|
Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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?

Schol-R-LEA
Veteran Poster
1,042 posts since Oct 2010
Reputation Points: 452
Solved Threads: 167
Skill Endorsements: 11

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.

Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Schol-R-LEA
Veteran Poster
1,042 posts since Oct 2010
Reputation Points: 452
Solved Threads: 167
Skill Endorsements: 11

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

seo_charlotte
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Chuckleluck
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1117 seconds using 2.76MB