c++ - class has no constructor forward declaration -


i'm trying create state engine in game different screens. id start screen, maybe file chooser in future, overworld map, menu screen, etc. when forward declare class says there no constructors.

gamestate.h:

#pragma once  class gamestate { public:     virtual ~gamestate() {}     virtual void update() {}     virtual void handleevents() {}     virtual void draw(graphics& gfx) {}      gamestate* getcurrentstate()     {         return currentstate;     }     void changestate(gamestate* state)     {         currentstate = state;     }  protected:     sdl_renderer* renderer;     gamestate* currentstate; }; 

gamestates.h

#pragma once #include "gamestate.h" #include "texture.h" #include "keyboard.h"  class titlegamestate; class introgamestate : public gamestate { public:     introgamestate(sdl_renderer* renderer, keyboardclient& kbd)         :         kbd(kbd)     {         background = new texture("red.png", renderer);         this->renderer = renderer;     }     ~introgamestate() {}     void handleevents()     {         while (!kbd.keyempty())         {             keyevent e = kbd.readkey();             switch (e.getcode())             {             case sdlk_return:                 if (e.ispress())                 {                     currentstate = new titlegamestate(renderer, kbd);                 }                 break;             }         }     }     void logic() {}     void draw(graphics& gfx)     {         background->draw(0, 0, gfx);     }  private:     texture* background;     keyboardclient& kbd; };  class titlegamestate : public gamestate { public:     titlegamestate(sdl_renderer* renderer, keyboardclient& kbd)         :         kbd(kbd)     {         background = new texture("blue.png", renderer);     }     ~titlegamestate() {}     void handleevents()     {         while (!kbd.keyempty())         {             keyevent e = kbd.readkey();             switch (e.getcode())             {             case sdlk_return:                 if (e.ispress())                 {                     printf("ok");                 }                 break;             }         }     }     void logic() {}     void draw(graphics&gfx)     {         background->draw(0, 0, gfx);     }  private:     texture* background;     keyboardclient& kbd; }; 

i define class right afterwards, , know can move above introgamestate, when implement menu game state, go , forth between menu state , overworld state. how can around this?

compiler error:

error c2514: 'titlegamestate' : class has no constructors file: gamestates.h line: 28 

line 28 line of code:

currentstate = new titlegamestate(renderer, kbd); 

the error on line currentstate = new titlegamestate(renderer, kbd); because compiler has yet see constructor class. doesn't know how compile code.

if forward declare class class titlegamestate; can declare pretty pointer titlegamestate*.

to compile code have, need define class before using. note: defining class not mean defining methods well. class definition consists of method , member declarations.

class titlegamestate : public gamestate { public:   titlegamestate(sdl_renderer*, keyboardclient&);   ~titlegamestate();   void handleevents();   // ... };  class introgamestate : public gamestate {   // ... } 

after definition of classes, can define member functions;

/*inline*/ void titlegamestate::handleevents() // inline needed if method defined in header file // avoid linker errors {   // ... } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -