Jump to content

  • Log In with Google      Sign In   
  • Create Account


Forward Declaration of Class breaks the game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 03:22 PM

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!
 


Sponsor:

#2 Vortez   Crossbones+   -  Reputation: 2697

Like
2Likes
Like

Posted 27 June 2013 - 03:25 PM

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



#3 Paradigm Shifter   Crossbones+   -  Reputation: 5252

Like
2Likes
Like

Posted 27 June 2013 - 03:34 PM

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, 27 June 2013 - 03:35 PM.

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

#4 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 03:49 PM

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 

 



#5 Vortez   Crossbones+   -  Reputation: 2697

Like
2Likes
Like

Posted 27 June 2013 - 04:09 PM

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, 27 June 2013 - 04:10 PM.


#6 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 04:17 PM

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



#7 Paradigm Shifter   Crossbones+   -  Reputation: 5252

Like
1Likes
Like

Posted 27 June 2013 - 04:23 PM

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

#8 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 04:29 PM

They are different pointer values.. What does that mean? I am passing "this"...
 
void CMainGame::OnUpdate()
{
 
MainCharacter->Update(this);
 
if (isKeyHold(SDLK_ESCAPE))
{
Quit();
}
}


#9 Paradigm Shifter   Crossbones+   -  Reputation: 5252

Like
1Likes
Like

Posted 27 June 2013 - 04:38 PM

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

#10 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 04:48 PM

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



#11 Paradigm Shifter   Crossbones+   -  Reputation: 5252

Like
2Likes
Like

Posted 27 June 2013 - 04:54 PM

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.


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

#12 Vortez   Crossbones+   -  Reputation: 2697

Like
2Likes
Like

Posted 27 June 2013 - 05:01 PM

<twilight zone music playing in the background>

 

0x00000026? that's weird... blink.png


Edited by Vortez, 27 June 2013 - 05:02 PM.


#13 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 05:03 PM

// 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'


#14 Vortez   Crossbones+   -  Reputation: 2697

Like
0Likes
Like

Posted 27 June 2013 - 05:15 PM

At this point i think you should post the whole thing



#15 NightCreature83   Crossbones+   -  Reputation: 2737

Like
1Likes
Like

Posted 27 June 2013 - 05:16 PM

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, 27 June 2013 - 05:18 PM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#16 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 05:37 PM

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


#17 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 05:46 PM

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.



#18 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 06:32 PM

I have just managed to fix it :) Thank you all for help. Problem wasn't with controls but with something else in my class :x Something very silly



#19 Khatharr   Crossbones+   -  Reputation: 2959

Like
1Likes
Like

Posted 27 June 2013 - 06:56 PM

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


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#20 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 27 June 2013 - 07:15 PM

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, 27 June 2013 - 07:16 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS