Jump to content
  • Advertisement
Sign in to follow this  
c4c0d3m0n

Compilation error: CGameEngine has not been declared

This topic is 3752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all. I'm just writing some code to try a new method of game programming using a state manager. I'm using this tutorial as a base. I'm pretty stuck though. I don't get the error, for I have included the correct header! Output
rob@fruchtsirup:~/Devel/gamestates$ make
g++ -c gameengine.cpp
In file included from gameengine.h:16,
                 from gameengine.cpp:13:
gamestate.h:32: error: ‘CGameEngine’ has not been declared
gamestate.h:33: error: ‘CGameEngine’ has not been declared
gamestate.h:36: error: ‘CGameEngine’ has not been declared
gamestate.h: In member function ‘void CGameState::changeState(int*, CGameState*)’:
gamestate.h:37: error: request for member ‘changeState’ in ‘* game’, which is of non-class type ‘int’
gameengine.cpp: In member function ‘void CGameEngine::handleEvents()’:
gameengine.cpp:103: error: no matching function for call to ‘CGameState::handleEvents(CGameEngine* const)’
gamestate.h:32: note: candidates are: virtual void CGameState::handleEvents(int*)
gameengine.cpp:107: error: invalid operands of types ‘const char [45]’ and ‘const char [7]’ to binary ‘operator+’
gameengine.cpp: In member function ‘void CGameEngine::update()’:
gameengine.cpp:116: error: no matching function for call to ‘CGameState::update(CGameEngine* const)’
gamestate.h:33: note: candidates are: virtual void CGameState::update(int*)
make: *** [gameengine.o] Error 1
Makefile
cc=g++
cflags=`sdl-config --cflags --libs`
files=gameengine.o main.o

# Linking objects
all : ${files}
	@echo 
	${cc} -g -o tetris ${files} ${cflags}
	@echo 
	@echo Completed

# Compiling sources
%.o : %.cpp
	${cc} -c $<

# Clean
clean :
	@\rm -f *.o *~ tetris
	@echo Cleaned
Sources:
/**
  * @author         Rob Spoel
  * @copyright      2008
  *
  * @file           gameengine.cpp
  * @description    GameEngine implementation
  *
  *
**/


#include "gameengine.h"

#include <iostream>
#include <stdexcept>
#include <string>



/// Constructor

CGameEngine::
CGameEngine()
    : m_running ( true )
{
    std::cout << "GameEngine init" << std::endl;
}


CGameEngine::
~CGameEngine()
{
    // Clean up states
    while ( !state_stack.empty() ) {
        state_stack.back()->destroy();
        state_stack.pop_back();
    }

    std::cout << "GameEngine destroyed..." << std::endl;
}




/// States

void
CGameEngine::
changeState( CGameState* state )
{
    // Clean up states
    while ( !state_stack.empty() ) {
        state_stack.back()->destroy();
        state_stack.pop_back();
    }

    // Start new state
    state_stack.push_back( state );
    state_stack.back()->init();
}


void
CGameEngine::
pushState( CGameState* state )
{
    // Pause last state
    if ( !state_stack.empty() )
      state_stack.back()->pause();

    // Store & start new state
    state_stack.push_back( state );
    state_stack.back()->init();
}


void
CGameEngine::
popState()
{
    // Clean up current state
    if ( !state_stack.empty() ) {
        state_stack.back()->destroy();
        state_stack.pop_back();
    }

    // Resume previous state
    if ( !state_stack.empty() )
      state_stack.back()->resume();
}




/// Logistics

void
CGameEngine::
handleEvents()
{
    if ( !state_stack.empty() )
      state_stack.back()->handleEvents( this );

    else
      throw std::runtime_error( "CGameEngine::handleEvents() : state_stack is"+
                                " empty" );
}


void
CGameEngine::
update()
{
    if ( !state_stack.empty() )
      state_stack.back()->update( this );

    else
      throw std::runtime_error( "CGameEngine::update() : state_stack is empty"
                              );
}



/// EOF

/**
  * @author         Rob Spoel
  * @copyright      2008
  *
  * @file           gameengine.h
  * @description    GameEngine header
  *
  *
**/


#ifndef GAME_ENGINE_H
#define GAME_ENGINE_H

#include "gamestate.h"
#include <vector>



class CGameEngine
{
  public:

    CGameEngine();
    ~CGameEngine();


    void changeState( CGameState* state );
    void pushState( CGameState* state );
    void popState();


    void handleEvents();
    void update();


    bool running() const { return m_running; }
    void quit() { m_running = false; }


  private:

    std::vector<CGameState*> state_stack;

    bool m_running;

};



#endif // GAME_ENGINE_H

/**
  * @author         Rob Spoel
  * @copyright      2008
  *
  * @file           gamestate.h
  * @description    GameState header
  *
  *
**/


#ifndef GAME_STATE_H
#define GAME_STATE_H

#include "gameengine.h"



class CGameState
{
  public:

    virtual void init()    = 0;
    virtual void destroy() = 0;


    virtual void pause()  = 0;
    virtual void resume() = 0;


    virtual void handleEvents( CGameEngine* game ) = 0;
    virtual void update( CGameEngine* game )       = 0;


    void changeState( CGameEngine* game, CGameState* state )
        { game->changeState( state ); }


  protected:

    CGameState() { }

};



#endif // GAME_STATE_H

Share this post


Link to post
Share on other sites
Advertisement
You are referring to CGameEngine in CGameState. CGameEngine has not been declared at the point you refer to it.

Unfortunately, you can't include "gameengine.h" in gamestate.h as you would create a circular dependancy.

What you can do is do:

class CGameEngine;

at the top of gamestate.h, and then include gameengine.h in gamestate.cpp, before the implementation of the gamestate methods.

However, because you have only forward declared CGameEngine in gamestate.h, you can only declare pointers or references to it - you can't call any methods since they are not defined.

So your inline CGameState::changeState() method is out and will need to be just prototyped in gamestate.h and actually implemented in gamestate.cpp, since it actually calls a method of CGameEngine.

Example:

gameengine.h

#include "gamestate.h"

class GameEngine
{
public:
void ArgleBargle(GameState *S){ S->DoSomething(); } // ok as GameState fully defined
};


;

gamestate.h

class GameEngine; // forward declaration

class GameState
{
public:
void DoSomething(){ }

void Ok(GameEngine *E); // ok as just a pointer to GameEngine
void NotOk(GameEngine *E){ E->ArgleBargle(this); } // no good as GameEngine not defined yet
};




gamestate.cpp

#include "gamestate.h"
#include "gameengine.h" // this is ok

void GameState::Ok(GameEngine *E)
{
E->ArgleBargle(this); // ok now, as GameEngine now defined
}




HTH

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
(...)

However, because you have only forward declared CGameEngine in gamestate.h, you can only declare pointers or references to it - you can't call any methods since they are not defined.

So your inline CGameState::changeState() method is out and will need to be just prototyped in gamestate.h and actually implemented in gamestate.cpp, since it actually calls a method of CGameEngine.

(...)


This is not really something I want, because CGameState is a prototype for future gamestate classes that will use it as its parent. Is there a possibility to do it the other way around, so I would forward declare CGameState in gameengine.h?


I'm quite bummed out that this is not valid code... I really liked the way I had everything set up, with small straight-forward functions declared in the header already. I guess I'll move those small CGameEngine functions to gameengine.cpp then...


Also, please ignore the crazy stuff I tried in my exception throwing code. That was just me being insane.


edit
I tried the forward declaration thingamagic myself, it works :) Thanks guys

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!