gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Base Pointer to a derived class

#174285 - Azenris - Wed May 26, 2010 8:25 pm

A little confused why I can't have this. Currently I get the error shown below

Code:
1>c:/Users/Azenris/Desktop/RedTemple/RedTemple/arm9/source/state_titlescreen.cpp(120): error: no matching function for call to 'CState_TitleScreen::MoveToState(CState_NewGame*)'
1>../nexlib_dir/include/state.h(74): note: candidates are: void IState::MoveToState(IState*, bool, int)


CState_TitleScreen inherits from IState where the MoveToState function exists. I thought I could use a pointer to the base, i did with with all my entities. They become players, enemies, object, special effects and all in a list of the base entity class.

Is it something to do with the function being apart of the base class? Hmm this is confusing me :(

EDIT: I changed it slighly moving control from the state to the statemanager but its still the same problem

Code:
1>c:/Users/Azenris/Desktop/RedTemple/RedTemple/arm9/source/state_splashscreen.cpp: In member function 'virtual void CState_SplashScreen::OnUpdate()':
1>c:/Users/Azenris/Desktop/RedTemple/RedTemple/arm9/source/state_splashscreen.cpp(68): error: no matching function for call to 'CStateManager::MoveToState(CState_TitleScreen*, bool, int)'
1>../nexlib_dir/include/state_manager.h(41): note: candidates are: void CStateManager::MoveToState(IState*, bool, int)


EDIT: here is some more info incase I didnt provide enough

state_splashscreen.h
Code:
// =================================================================================
// CLASS: CState_SplashScreen
class CState_SplashScreen : public IState
{
    // other stuff
}


state_manager.h
Code:
// ================================================================================
// CLASS: CStateManager
class CStateManager : public IManager
{
    // other stuff
    void MoveToState(IState *pNewState, bool fade = true, int speed = 8);
}


state_manager.cpp
Code:
// ================================================================================
// MoveToState
// @ pNewState:      state to go next
// @ fade:         whether or not to fade out
// @ speed:         speed to fade out
void CStateManager::MoveToState(IState *pNewState, bool fade, int speed)
{
   m_pStateToMove = pNewState;

   if (fade)
   {
      m_pCurrentState->FadeOut(SCREEN_MAIN, FADE_BLACK, speed);
      m_pCurrentState->FadeOut(SCREEN_SUB, FADE_BLACK, speed);
   }
   else
   {
      m_pCurrentState->SetMoveOn();
   }
}


game.h
Code:
class CState_SplashScreen;

class CGame
{
     // other stuff
    CState_TitleScreen *GetState_TitleScreen(void) const;
}


game.cpp
Code:
#include "state_splashscreen.h"
CState_TitleScreen *CGame::GetState_TitleScreen(void) const {return m_pState_TitleScreen;}


the error call
Code:
g_pGame->GetStateManager()->MoveToState(g_pGame->GetState_TitleScreen(), true, 4);

_________________
<Robert Morley>
My Homebrew Games


Last edited by Azenris on Wed May 26, 2010 9:14 pm; edited 1 time in total

#174286 - Miked0801 - Wed May 26, 2010 9:14 pm

Offhand, it looks like you are passing different parameters to each function?

#174287 - Azenris - Wed May 26, 2010 9:18 pm

But the states are all

Code:
<whatever state> : public IState


So passing in <whatever state> should be ok for a IState * parameter

EDIT:

if I change the
Code:
CState_TitleScreen *CGame::GetState_TitleScreen(void) const {return m_pState_TitleScreen;}
to
Code:
IState *CGame::GetState_TitleScreen(void) const {return m_pState_TitleScreen;}


It will work, but I dont want to return the base pointer I want it as the class it is. I don't get why it wouldnt work. Guess im misunderstanding something.

EDIT: ok I get it now, its because I was using forward declarations, thats the only time they saw the class and so didnt know they inherited from the IState, would be nice if you could forward declare that :(

anyway just added includes in the files the calls from state-to-state were made and now they 'see' they are infact from IState.

ty for any help
_________________
<Robert Morley>
My Homebrew Games

#174305 - Miked0801 - Thu May 27, 2010 7:12 pm

Not to toot another board's horn or anything, but stackoverflow.com is an excellent resource for language specific questions and very quick answers.