Join 135,942 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,725 people online right now. Registration is fast and FREE... Join Now!
I've been trying to figure out for quite some time what is wrong with this piece of code, but I still didn't find a solution.
Below you see the temporary header file code to which the errors point:
cpp
// ----------------------- // // >>>>> SONIC CLASS <<<<< // // ----------------------- //
#pragma once
// >> Sonic - INCLUDE HEADERS AND/OR LIBRARIES << #include "GameEngine.h" // Include Game Engine v2.04 header. #include "Attributes.h" // Include Attributes class header. #include "Level.h"
class Sonic { public: // >> Sonic - CONSTRUCTOR << Sonic(); // Prototype of the constructor used in Sonic.cpp.
// >> Sonic - DESTRUCTOR << virtual ~Sonic(); // Prototype of the destructor used in Sonic.cpp.
// >> Sonic - FUNCTIONS << // Prototypes of the functions used in Sonic.cpp. void GameStart(); void GameEnd(); void CheckKeyboard(); void GameCycle(RECT rect);
private: // >> Sonic - DECLARATION OF DATA MEMBERS <<
// Declarations of all data members of datatype "Bitmap". Bitmap * m_BmpSonicRegularRightPtr, * m_BmpSonicRegularLeftPtr;
// Declarations of all data members of datatype "bool". bool m_SonicRegularRight, m_SonicRegularLeft;
Attributes * m_AttributesPtr; // Class pointer used to refer to the class Attributes. Level * m_LevelPtr;
This gives me 12 errors and all of them point to the same line (line 38 above):
cpp
Level * m_LevelPtr;
Errors:
QUOTE
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Error 1 error C2143: syntax error : missing ';' before '*' Error 4 error C2143: syntax error : missing ';' before '*' Error 7 error C2143: syntax error : missing ';' before '*' Error 10 error C2143: syntax error : missing ';' before '*'
I really don't understand why it is giving me these errors. I did the same thing with Attributes (Attributes * m_AttributesPtr;) and this works fine...
Since you're only making use of a pointer to Level, you could get away with just giving a prototype for Level in that file. Above your class definition for 'Sonic', have:
You did include "Level.h", which I assume has the Level class or structure. Did you make a spelling error, or capitalization error?
Yes, "Level.h" contains the Level class. No spelling errors or capitalization errors made. Haven't since my 10th birthday.
QUOTE(Voire @ 18 May, 2008 - 06:15 PM)
Since you're only making use of a pointer to Level, you could get away with just giving a prototype for Level in that file. Above your class definition for 'Sonic', have:
CODE
class Level;
class Sonic { ...
I had already tried to add "class Level;" instead of "#include "Level.h"", but then it gives me the following 3 errors and 1 warning:
QUOTE
Warning 2 warning C4150: deletion of pointer to incomplete type 'Level'; no destructor called Error 1 error C2512: 'Level' : no appropriate default constructor available Error 4 error C2227: left of '->m_Y' must point to class/struct/union/generic type Error 3 error C2027: use of undefined type 'Level'
None of the suggestions listed above make my Level class pointer work.
// >> Level - INCLUDE HEADERS AND/OR LIBRARIES << #include "GameEngine.h" // Include Game Engine v2.04 header. #include "Outro.h" // Include Outro class header. #include "Sonic.h" // Include Sonic class header. #include "Attributes.h" // Include Attributes class header.
class Level : public Callable { friend class Sonic; public: // >> Level - CONSTRUCTOR << Level(); // Prototype of the constructor used in Level.cpp.
// >> Level - DESTRUCTOR << virtual ~Level(); // Prototype of the destructor used in Level.cpp.
// >> Level - FUNCTIONS << // Prototypes of the functions used in Level.cpp. void GameStart(); void GameEnd(); void GameCycle(RECT rect); void CallAction(Caller* c);
private: // >> Level - DECLARATION OF DATA MEMBERS <<
Outro * m_OutroPtr; // Class pointer used to refer to the class Outro. Sonic * m_SonicPtr; // Class pointer used to refer to the class Sonic. Attributes * m_AttributesPtr; // Class pointer used to refer to the class Attributes.
// Declarations of all data members of datatype "Bitmap". Bitmap * m_BmpTestPtr, * m_BmpTest2Ptr, * m_BmpScorePtr, * m_BmpRings1Ptr, * m_BmpRings2Ptr;
// Declarations of all data members of datatype "bool". bool m_Zone1, m_Zone2, m_Rings1, m_Rings2;
// Declarations of all data members of datatype "Timer". Timer * m_RingsTimerPtr;
// >> Level - INCLUDE HEADERS AND/OR LIBRARIES << #include "Level.h" // Include Level class header.
// >> Level - DEFINES << #define GAME_ENGINE (GameEngine::GetSingleton()) // Retrieve access to Game Engine v2.04.
// >> Level - CONSTRUCTOR << Level::Level():m_OutroPtr(0), m_SonicPtr(0), m_AttributesPtr(0), m_BmpTestPtr(0), m_BmpTest2Ptr(0), m_BmpScorePtr(0), m_BmpRings1Ptr(0), m_BmpRings2Ptr(0), m_Zone1(true), m_Zone2(false), m_Rings1(true), m_Rings2(false), m_RingsTimerPtr(0), m_Y(0) // Initializing data members. { m_OutroPtr = new Outro(); // Create a new instance of the class Outro with "new" and link it to m_OutroPtr. Use m_OutroPtr to refer to the class Outro. m_SonicPtr = new Sonic(); // Create a new instance of the class Sonic with "new" and link it to m_SonicPtr. Use m_SonicPtr to refer to the class Sonic. m_AttributesPtr = new Attributes(); // Create a new instance of the class Attributes with "new" and link it to m_AttributesPtr. Use m_AttributesPtr to refer to the class Attributes. }
// >> Level - DESTRUCTOR << Level::~Level() { // Delete all the class pointers that were created with "new". One delete per line. delete m_OutroPtr; delete m_SonicPtr; delete m_AttributesPtr; }
When the compiler is compiling Level.h, Sonic.h is included. From Sonic.h, you reinclude Level.h. Since you have #pragma once, Level.h is not reincluded. At this point, you have just included GameEngine.h and Outra.h, but Sonic.h needs the the class Level (which is below the actual code).
So, this is basically what the compiler is working with.
// >> Level - INCLUDE HEADERS AND/OR LIBRARIES << #include "GameEngine.h" // Include Game Engine v2.04 header. #include "Outro.h" // Include Outro class header.
// ----------------------- // // >>>>> SONIC CLASS <<<<< // // ----------------------- //
// >> Sonic - INCLUDE HEADERS AND/OR LIBRARIES << #include "GameEngine.h" // Include Game Engine v2.04 header. #include "Attributes.h" // Include Attributes class header. //#include "Level.h" // removed due to pragma once
class Sonic { public: // >> Sonic - CONSTRUCTOR << Sonic(); // Prototype of the constructor used in Sonic.cpp.
// >> Sonic - DESTRUCTOR << virtual ~Sonic(); // Prototype of the destructor used in Sonic.cpp.
// >> Sonic - FUNCTIONS << // Prototypes of the functions used in Sonic.cpp. void GameStart(); void GameEnd(); void CheckKeyboard(); void GameCycle(RECT rect);
private: // >> Sonic - DECLARATION OF DATA MEMBERS <<
// Declarations of all data members of datatype "Bitmap". Bitmap * m_BmpSonicRegularRightPtr, * m_BmpSonicRegularLeftPtr;
// Declarations of all data members of datatype "bool". bool m_SonicRegularRight, m_SonicRegularLeft;
Attributes * m_AttributesPtr; // Class pointer used to refer to the class Attributes. Level * m_LevelPtr;
Sonic(const Sonic& s); Sonic& operator=(const Sonic& s); }; // END OF SONIC.H
#include "Attributes.h" // Include Attributes class header.
class Level : public Callable { friend class Sonic; public: // >> Level - CONSTRUCTOR << Level(); // Prototype of the constructor used in Level.cpp.
// >> Level - DESTRUCTOR << virtual ~Level(); // Prototype of the destructor used in Level.cpp.
// >> Level - FUNCTIONS << // Prototypes of the functions used in Level.cpp. void GameStart(); void GameEnd(); void GameCycle(RECT rect); void CallAction(Caller* c);
private: // >> Level - DECLARATION OF DATA MEMBERS <<
Outro * m_OutroPtr; // Class pointer used to refer to the class Outro. Sonic * m_SonicPtr; // Class pointer used to refer to the class Sonic. Attributes * m_AttributesPtr; // Class pointer used to refer to the class Attributes.
// Declarations of all data members of datatype "Bitmap". Bitmap * m_BmpTestPtr, * m_BmpTest2Ptr, * m_BmpScorePtr, * m_BmpRings1Ptr, * m_BmpRings2Ptr;
// Declarations of all data members of datatype "bool". bool m_Zone1, m_Zone2, m_Rings1, m_Rings2;
// Declarations of all data members of datatype "Timer". Timer * m_RingsTimerPtr;
However, the "friend class Sonic;" is needed because I do access one of the private data members. Just checked it again, and when I remove it, I get an error saying one of the private members could not be accessed. So it needs to be there.
EDIT: It only compiles without errors. When I run it, it crashes with "Stack overflow" error.
This post has been edited by skater_00: 18 May, 2008 - 02:27 PM