lundi 20 avril 2015

State pattern - c++

Im trying to implement a Finite State Machine with the State Design Pattern, im very new to c++ and a beginner programmer in general. includes and header files confuse me and I'm not quite sure how to correctly set up this project. I probably should start with something else but I don't really have a choice.

The goal for the machine is to read a char and, depending on the State, do something different.

This is the class layout i thought of:

Machine.h

class Machine{
   void setCurrentState(State s);
   State currentState;
}

State.h

class State{
   virtual void read(char c, Machine* m) = 0;
}

(Should i make a header file for each derived class?)

Initial.h

class Initial: public State{
   void read(char c, Machine* m){
      m->setCurrentState(new SomeState());
   }
}

I tried to do it that way but I'm confused on what I should include where. I'm getting, depending on which way I do it wrong errors from type not defined to undefined reference vtable. Maybe you guys could help me set this up correct and get the linking on point, id appreciate it a lot.

Aucun commentaire:

Enregistrer un commentaire