Forward Declaration of Class breaks the game

Started by
20 comments, last by NightCreature83 10 years, 9 months ago
Hello, I've been stuck on this for couple hours now. Help would be hugely apprenticed :)
I've cut most of the code out to make it less confusing but I'm having problem passing CMainGame class into a MainCharacters class function. If I do forward declaration of the CMainGame it doesn't error out, but crashes as soon as I use one of the functions from the CMainGame.

// MainCharacter.h ----------------------------------------------------------------------------------------------------------------
#pragma once
#include "stdafx.h"
#include <engine.h>
#include "collisionListener.h"
#include "MainGame.h"
class CMainGame; //if this forward declaration of class is added it fixes it and game runs with out errors until I run one of the functions from this class in Update function.
class CMainCharacter
{
public:
CMainCharacter(void);
CMainCharacter(CPhysics* Physics, collisionListener* theCollisionListener);
~CMainCharacter(void);
void Update(CMainGame* theGame); // error C2061: syntax error : identifier 'CMainGame'
private:
CSprite* MainCharacter;
CSprite* Sword;
};
//MainGame.h ----------------------------------------------------------------------------------------------------------------
#pragma once
#include <engine.h>
#include <Mathematics.h>
#include "Enemy.h"
#include "MainCharacter.h"
#include "collisionListener.h"
class CMainGame : public engine, Mathematics
{
public:
CMainGame();
~CMainGame(void);
virtual void OnDraw();
virtual void OnUpdate();
private:
collisionListener* theCollisionListener;
CMainCharacter* MainCharacter;
};
----------------------------------------------------------------------------------------------
Thank you for help in advance!
Advertisement

Did you tried debugging it and see where it crash? My guess is a null or uninitialized pointer somewhere.

Yep, use the debugger. Is the theGame pointer valid when you call CMainCharacter::Update?

Also, you will want a virtual destructor for CMainGame since you have virtual functions in the class.

And why is a CMainGame derived from Mathematics? The game IS A Mathematics??? I suspect that is an interface though (i.e. all virtual functions, so the game implements Mathematics), but I can't see any reason why maths functions would be in a virtual interface...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Mathematics class just contains random math functions like getting distance, random numbers etc.

Oh I didn't know I need virtual constructor when I have virtual functions. Thank you I'll add that.

void CMainCharacter::Update(CMainGame* theGame)
{
SDL_GetMouseState(&Mouse_x, &Mouse_y);
if (theGame->isKeyHold(SDLK_d)) //breaks on this line
{
....
}
I've been dubbging it and the thing is theGame isn't null, it has values. It shouldn't break in that function because I have this in CMainGame's Update function:
if (isKeyHold(SDLK_ESCAPE))
{
Quit();
}
//and that works

What value does theGame pointer have? It might be initialized to 0xCDCDCDCD or something like that, in which case, it's not null, but is not pointing to valid memory location either.

Did you make sure those pointers are set to NULL when your program start?

theGamepointer value is 0x00a24c20 just before it hits line "if (theGame->isKeyHold(SDLK_d))"

Is that the same as the 'this' pointer from any of your CMainGame functions while they are executing?

How are you calling CMainCharacter::Update?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
They are different pointer values.. What does that mean? I am passing "this"...
void CMainGame::OnUpdate()
{
MainCharacter->Update(this);
if (isKeyHold(SDLK_ESCAPE))
{
Quit();
}
}

Hmm, I dunno. The this pointer passed to MainCharacter::Update should be the same as the theGame pointer received inside CMainCharacter::Update...

Have you tried doing a full rebuild?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I've just noticed that "this" is actually null. I thought 0x000026 is good pointer. But why is that?

Heres a picture :)

NULL_CLASS.png

This topic is closed to new replies.

Advertisement