Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualAshaman73

Posted 10 January 2013 - 04:06 AM

To solve the header problem, independently of this is good or bad coding style, you just need to forward declare the class instead of including its header. In this case you can use it in the header files, as long as you don't access it there (e.g. in inline code):

 

class MainMenu;
class Game;

    class GameState
    {
    public:
    virtual void HandleEvents() = 0;
    virtual void logic() = 0;
    virtual void render() = 0;
    virtual ~GameState(){};
// new=>
     MainMenu myMainMenu;     


    void SetNextState(GameStates newState); // State status manager
    void ChangeState(); // State changer
     
    private:
    };

 

But this is not the problem here, you can just include all headers in your cpp like this:

#include "GameState"
#include "Game"
#include "MainMenu"


    void GameState::ChangeState()
    {
    if (nextState != STATE_NULL)
    {
    if (nextState != STATE_EXIT)
    delete currentState;
     
    switch (nextState)
    {
    case STATE_INTRO: break;
    case STATE_MENU: currentState = new MainMenu(); break;
    case STATE_GAME: currentState = new Game(32, 32); break;
    }
     
    nextState = STATE_NULL;
    }
    }
     
    void GameState::SetNextState(GameStates newState)
    {
    nextState = newState;
    }

 

Summary  of headers/cpps/includes:

Game.h
---------
include "GameState.h"

Game.cpp
------------
include "Game.h"

MainMenu.h
---------
include "GameState.h"

MainMenu.cpp
------------
include "MainMenu.h"

GameState.h
---------
no include

GameState.cpp
------------
include "GameState.h"
include "MainMenu.h"
include "Game.h"


#2Ashaman73

Posted 10 January 2013 - 04:02 AM

To solve the header problem, independently of this is good or bad coding style, you just need to forward declare the class instead of including its header. In this case you can use it in the header files, as long as you don't access it there (e.g. in inline code):

 

class MainMenu;
class Game;

    class GameState
    {
    public:
    virtual void HandleEvents() = 0;
    virtual void logic() = 0;
    virtual void render() = 0;
    virtual ~GameState(){};
// new=>
     MainMenu myMainMenu;     


    void SetNextState(GameStates newState); // State status manager
    void ChangeState(); // State changer
     
    private:
    };

 

But this is not the problem here, you can just include all headers in your cpp like this:

#include "GameState"
#include "Game"
#include "MainMenu"


    void GameState::ChangeState()
    {
    if (nextState != STATE_NULL)
    {
    if (nextState != STATE_EXIT)
    delete currentState;
     
    switch (nextState)
    {
    case STATE_INTRO: break;
    case STATE_MENU: currentState = new MainMenu(); break;
    case STATE_GAME: currentState = new Game(32, 32); break;
    }
     
    nextState = STATE_NULL;
    }
    }
     
    void GameState::SetNextState(GameStates newState)
    {
    nextState = newState;
    }

#1Ashaman73

Posted 10 January 2013 - 04:00 AM

To solve the header problem, independently of this is good or bad coding style, you just need to forward declare the class. In this case you can use it in the header files, as long as you don't access it there (e.g. in inline code):

 

class MainMenu;
class Game;

    class GameState
    {
    public:
    virtual void HandleEvents() = 0;
    virtual void logic() = 0;
    virtual void render() = 0;
    virtual ~GameState(){};
// new=>
     MainMenu myMainMenu;     


    void SetNextState(GameStates newState); // State status manager
    void ChangeState(); // State changer
     
    private:
    };

 

But this is not the problem here, you can just include all headers in your cpp like this:

#include "GameState"
#include "Game"
#include "MainMenu"


    void GameState::ChangeState()
    {
    if (nextState != STATE_NULL)
    {
    if (nextState != STATE_EXIT)
    delete currentState;
     
    switch (nextState)
    {
    case STATE_INTRO: break;
    case STATE_MENU: currentState = new MainMenu(); break;
    case STATE_GAME: currentState = new Game(32, 32); break;
    }
     
    nextState = STATE_NULL;
    }
    }
     
    void GameState::SetNextState(GameStates newState)
    {
    nextState = newState;
    }

PARTNERS