jeudi 6 août 2015

C++ State pattern exception

I'm new in c++! Please help me to implement State pattern.

I have the code below, but it doesn't compile:

class MasterThreadState;
class Init; class Idle;

class MasterThread
{
public:
    MasterThread(){
        _state = Init::State();
    }
    void handle(int sender, int tag);
private:
    int _sender;
    MasterThreadState* _state;
private:
    friend class MasterThreadState;
    void goTo(MasterThreadState* newState){ _state = newState; }
};

class MasterThreadState
{
public:
    virtual void recieved(MasterThread& master, int tag);
protected:
    void goTo(MasterThread& m, MasterThreadState* newState){
        m.goTo(newState);
    }
};

class Init : MasterThreadState {
public:
    static MasterThreadState& State() { return instance; }
    virtual void recieved(MasterThread& master, int tag);
private:
    static Init instance;
};

class Idle : MasterThreadState {
public:
    void recieved(MasterThread& master,int tag);
    static MasterThreadState& State(){ return instance; }
private:
    static Idle instance;
};

The error is:

incomplete type 'Init' used in nested name specifier _state = Init::State();

Aucun commentaire:

Enregistrer un commentaire