visual c++ - Understanding Abstraction in C++ -


coming java c++ i'm attempting understand abstraction through object orientation.

to put practical example, developing small game using sfml library graphics. question not relate that, think of background info. anyway, way game works process through number of different states. in case 2:

  1. the menu state: menu of game drawn , game begin here.

  2. the game state: state controls game, update entities , draw them.

in order have created following classes:

gamestatemanager.h

#ifndef gamestatemanager_h #define gamestatemanager_h  #include <sfml/graphics.hpp> #include <iostream> #include "gamestate.h"  class gamestatemanager { public:     // constructor     gamestatemanager();     // state variables     static const int numgamestates = 2;     static const int menustate = 0;     static const int gamestate = 1;     // public functions     void set_state(int state);     void update();     void draw(sf::renderwindow &win);     void input(sf::event event);  private:     // array of gamestates     gamestate game_states[];     // current state     int current_state;     // private functions     void load_state(int state);     void unload_state(int state); };  #endif 

gamestate.h

#ifndef gamestate_h #define gamestate_h  #include <iostream> #include <sfml/graphics.hpp> #include "gamestatemanager.h"  class gamestate { protected:     gamestatemanager gsm; public:     virtual void init() = 0;     virtual void update() = 0;     virtual void draw(sf::renderwindow &win) = 0;     virtual void input(sf::event event) = 0; };  #endif 

now may have noticed array of gamestates in game state manager? provides error not understand: zero-sized array. mean initialization needs made within header file? further point compiler mentions array of abstract class isn't allowed?

the second issue field gsm in abstract gamestate class not recognize , brings yet error: missing type specifier.

now complicate things further have following class: menustate. class meant extend gamestate.

menustate.h

#ifndef menustate_h #define menustate_h  #include "gamestate.h"  class menustate: public gamestate { public:     menustate(gamestatemanager gsm);     void init();     void update();     void draw(sf::renderwindow &win);     void input(sf::event event); private:     sf::texture title_texture;     sf::sprite title_sprite; };  #endif 

as mentioned class control menu of game.

implementing gamestatemanager done follows:

gamestatemanager.cpp

/*  * gamestate manager take care of various states of game.  * in particular there 2 states: menu or ingame. gamestatemanager  * load , unload each state needed.  *  * author: ben euden  * date: 2/5/2014  */  #include "gamestatemanager.h"  // class constructor gamestatemanager::gamestatemanager() {      game_states = game_states[numgamestates];      current_state = menustate;     load_state(current_state); }  /*  * load current game creating , initialising state  * storing in game_states array.  * @param state state wish load.  */ void gamestatemanager::load_state(int state) {     if(state == menustate)         game_states[state] = menustate(this);     //if(state == gamestate)         //game_states[state] = maingamestate(this);    // not implemented yet. }  /*  * unload state loaded load_state  */ void gamestatemanager::unload_state(int state) {     game_states[state] = null; }  void gamestatemanager::set_state(int state) {     unload_state(state);     current_state = state;     load_state(state); }  void gamestatemanager::update() {     try{         game_states[current_state].update();     }     catch(int e)     {         std::cout << "exception occured during update of game state" << e << std::endl;     } }  void gamestatemanager::draw(sf::renderwindow &win) {     try{         game_states[current_state].draw(&win);     }     catch(int e)     {         std::cout << "exception occured when trying draw gamestate: " << current_state << "exception number: " << e << std::endl;     } }  void gamestatemanager::input(sf::event event) {     game_states[current_state].input(event); } 

and menustate follows:

/*  * class extends game state header , deal menu of game  * includes drawing correct text screen, moving selector ,  * either exiting, bringing or starting game.  *  * author: ben euden  * date: 2/5/2014  */  #include "menustate.h"   menustate::menustate(gamestatemanager gsm) {     gsm = gsm;     init();  }  void menustate::init() {     title_texture = sf::texture();     title_texture.loadfromfile("sprites/title.png");     title_sprite = sf::sprite();     title_sprite.settexture(title_texture);     title_sprite.setposition(512, 200); }  void menustate::update(){}  void menustate::draw(sf::renderwindow &win) {     win.draw(title_sprite); }  void menustate::input(sf::event event) {  } 

please ignore inplemented methods , positionings. @ point began attempt compile project (i'm using visual studio) when errors appeared.

now in understand maingamestate hasn't been implemented yet menustate i'm sure i'm missing vital here still learning c++. in mind please excuse breakage of conventions etc again learning feel free correct me, better learn right way rather develop bad habits.

in summary i'd understand why receiving following errors:

protected:     gamestatemanager gsm; 

this produces error: missing ';' before gsm.

gamestate game_states[]; 

produces errors of: zero-size array, array of abstract class not allowed.

i believe if fix these rest sort out.

thank patience, time , assistance this.

euden

to short: don't know basics of c++, , beginner, should aproach totally different language java or c, should stop project right , find book c++ beginners. don't try mix java knowledge , fill gaps reach c++ knowledge, not work because if syntaxe close, different beasts.

i recommend learning c++ new , different language, whatever background. right doing big errors shows you're on wrong path learn c++. should basic tutorials (i'm not trying harsh, need learn basics before managing compile code).

you use of arrays , members if references shows lack of understanding of "value semantic" , several other basic concepts must-known of c++ usage.

for example, if have

class {    int k = 42;  // c++11 }; 

here a object contain k object. mean k not pointer int, it's actual value, within memory allocated object.

so if have

a my_object; // object on stack 

then my_object object taking size of int. if do:

class b {    int u;    a; }; 

then instance of b size of a object, plus size of int. b objects contain these data in single block of memory.

so when do:

class gamestate {     protected:     gamestatemanager gsm; 

what here build full gamestatemanager gamestate object. yes, gsm not reference, it's full object. should here either use c++ reference (if game manager should never change) or use pointer (or smart poitner if there ownership involved).

i see lot of other problems, array member gamestatemanager have absolutely not same meaning in java. basically, you're note coding in c++ here. (and should use either std::vector or std::array gamestate dynamic vector or array of pointers - or map or container).

as there point, should core point:

whatever language have learnt before, c or java related, never ever assume know of c++ yet, absolutely don't. need approach beginner. learn new language.

and make sure read material the list provided there. it's extremely easy learn bad practice online c++, unfortunately (but gets better).

also, might want read this: https://softwareengineering.stackexchange.com/questions/76675/how-can-a-java-programmer-make-the-most-of-a-new-project-in-c-or-c/76695#76695

by way, related recommendation: read "sfml game development" book example of simpler , safer (and c++-idiomatic) ways trying achieve here.

another side recommendation avoid using "manager" in type names, makes things hard understand , design.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -