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?
Chuckleluck
Junior Poster in Training
53 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
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
53 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
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
53 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
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
53 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
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
53 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
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
53 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
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
53 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1