Sign in to follow this  
iLoveGameProgramming

Forward Declaration of Class breaks the game

Recommended Posts

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!
 

Share this post


Link to post
Share on other sites

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

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

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 

 

Share this post


Link to post
Share on other sites
Vortez    2714

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?

Edited by Vortez

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

How do you create your CMainGame object? Put a breakpoint in your CMainGame constructor and your CMainGame::OnUpdate funtion and make sure the constructor is called before CMainGame::OnUpdate is called.

 

Looks like you aren't creating the CMainGame object before calling methods on it.

Share this post


Link to post
Share on other sites
// This is literally how its created
 
#include "stdafx.h"
#include <engine.h>
#include "MainGame.h"
 
int main(int argc, char* argsv[])
{
 
CMainGame* theGame = new CMainGame();
 
theGame->Run();
 
return 0;
}
 
when I put breakpoints in the constructor and on update, first it breaks at constructor. And everything exists then, it is not null. When I press F5 it goes to the Update and then its 0x0000026 again. Could it be because of this in MainCharacter.h ?
 
 
class CMainGame;
 
class CMainCharacter
{
public: 
 
But when I remove that line I get that:  
error C2061: syntax error : identifier 'CMainGame'

Share this post


Link to post
Share on other sites
NightCreature83    5002

Just as a curiosity but you do include CMainGame in the cpp file of CMainCharacter right? Because as soon as you want to use any of the function of CMainGame in CMainCharacter it needs to know the type properly so you can't call any function on it in the header.

 

Also are you compiling debug or release, because if you are in release the Camera function and AI update function calls before hand can invalidate the debuggers view of the this pointer, and you will have to inspect the disassembly to see which register is holding the this pointer to obtain valid values for it.

Edited by NightCreature83

Share this post


Link to post
Share on other sites

Just as a curiosity but you do include CMainGame in the cpp file of CMainCharacter right? Because as soon as you want to use any of the function of CMainGame in CMainCharacter it needs to know the type properly so you can't call any function on it in the header.

No CMainGame is included in .h file, and CMainCharacter .cpp only includes CMainCharacter.h. Ive just added it to cpp and still does the same thing
 
Also I was compiling in Release mode, my bad :(. In debug mode still crashing. The pointers in Update are working correctly in Debug mode... So it is not null or 0x000026... The constructor, update in main game and update in main character, the pointer values are all the same.
 
I'm not giving up until this is going to work... <twilight zone music playing in the background> 
that music doesn't help either hehe

Share this post


Link to post
Share on other sites

basically all I am trying to do is add controls functions to the MainCharacter class. I might be doing it wrong but in my engine I just inherit controls class into the main engine class. Ill just stop inheriting it and create it manually in my game, and pass that into the MainCharacter class.

Share this post


Link to post
Share on other sites

Could you explain what the problem was so that people looking at the same problem in the future can find the solution?

 

 

 

I've rewritten controls a little bit but this was the problem: 

 

CMainCharacter::CMainCharacter(CPhysics* Physics, collisionListener* theCollisionListener, controls* passedControls) 
{
theControls = passedControls;

theCollisionListener = theCollisionListener;

....

 

theCollisionListener was set to itself instead of 

this->theCollisionListener theCollisionListener;

 

Then it was breaking on 

 

if (theControls->isKeyHold(SDLK_a))  ///this line!

{
if (!theCollisionListener->GetAllowJump()) //but actual problem was on this line since this was null....
{
 
so it completely threw me off, and I was looking for the problem with controls...
 
Over 7 hours wasted trying to solve this stupid silly mistake!
Edited by iLoveGameProgramming

Share this post


Link to post
Share on other sites

Don't name your parameters the same thing as your class members then.

 

If you don't like m_thing, mThing, thing_ etc. you could use theListener for the member and aListener for the argument (parameter) to a function.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
NightCreature83    5002

And stop debugging in release mode as it has all kinds of optimisations on and shuffles the code around quite a bit to make it faster or smaller executable size. This means that the crash you had doesn't necessarily have to happen on the line that the debugger is indicating. Looking at the callstack becomes important then to figure out where it was coming from and what it was supposed to do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this