0

I am doing a sample implementation of a state machine in c++. I am facing issues during compilation regarding circular dependency of the classes. But I do not know how to resolve the issue, I have declared the classes in advance but still the program is not compiling. Help is really appreciated.

#include <iostream>

using namespace std;

class State;
class NO_CALLS;
class FIRST_INCOMING;
class ALL_CALLS_TERMINATED;
class FIRST_ACTIVE;
class CallsMachine;

class CallsMachine
{
  State *currentState;
  public:
    CallsMachine()
    {
        cout << "CallsMachine"<<endl;
        currentState = new NO_CALLS();
    }
    void setCurrent(State *s)
    {
        currentState = s;
    }
    void acceptCall()
    {
        currentState->acceptCall(this);
    }
    void rejectCall()
    {
        currentState->rejectCall(this);
    }
};

class State
{
  public:
    //These might as well contain blank implementation.

    virtual void acceptCall(CallsMachine *m /*, the second param should be the cCall object*/)
    {
        cout<<"State: acceptCall"<<endl;
    }
    virtual void rejectCall(CallsMachine *m)
    {
        cout<<"State: rejectCall"<<endl;
    }
};

class NO_CALLS: public State
{
  public:
    NO_CALLS()
    {
        cout <<"NO_CALLS"<<endl;
    };
    ~NO_CALLS()
    {
        cout <<"~NO_CALLS"<<endl;
    };
    void incomingCall(CallsMachine *m)
    {
        cout<<"FIRST_INCOMING: acceptCall"<<endl;
        m->setCurrent(new FIRST_INCOMING());
        //delete this;
    }
};

class ALL_CALLS_TERMINATED: public State
{
  public:
    ALL_CALLS_TERMINATED()
    {
        cout <<"ALL_CALLS_TERMINATED"<<endl;
    };
    ~ALL_CALLS_TERMINATED()
    {
        cout <<"~ALL_CALLS_TERMINATED"<<endl;
    };
};

class FIRST_INCOMING: public State
{
  public:
    FIRST_INCOMING()
    {
        cout <<"FIRST_INCOMING"<<endl;
    };
    ~FIRST_INCOMING()
    {
        cout <<"~FIRST_INCOMING"<<endl;
    };
    void acceptCall(CallsMachine *m)
    {
        cout<<"FIRST_INCOMING: acceptCall"<<endl;
        m->setCurrent(new FIRST_ACTIVE());
        //delete this;
    }
    void rejectCall(CallsMachine *m)
    {
        cout << "FIRST_INCOMING: rejectCall"<<endl;
        m->setCurrent(new ALL_CALLS_TERMINATED());
        //delete this;
    }
};

class FIRST_ACTIVE: public State
{
  public:
    FIRST_ACTIVE()
    {
        cout <<"FIRST_ACTIVE"<<endl;
    };
    ~FIRST_ACTIVE()
    {
        cout <<"~FIRST_ACTIVE"<<endl;
    };
    void acceptCall(CallsMachine *m)//The second call it can accept.
    {
        cout<<"FIRST_ACTIVE: acceptCall"<<endl;
        //m->setCurrent(new FIRST_ONHOLD_SECOND_ACTIVE());
        m->setCurrent(new ALL_CALLS_TERMINATED());
        //delete this;
    }
    void rejectCall(CallsMachine *m)
    {
        cout << "FIRST_ACTIVE: rejectCall"<<endl;
        m->setCurrent(new ALL_CALLS_TERMINATED());
        //delete this;
    }
};


int main()
{

}
Tanvir
  • 51
  • 8
  • 2
    http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c – SingerOfTheFall Oct 07 '15 at 10:27
  • 1
    It seems pointless to forward declare your classes and then define them immediately following those lines. Why not just define `State` first and then `CallsMachine` and then the rest? – EdChum Oct 07 '15 at 10:27
  • Hi EdChum, I did that in an attempt to resolve the dependency issue, however keeping them or removing them does not solve the issue. – Tanvir Oct 07 '15 at 10:29
  • 2
    @EdChum, because they all need to be fully defined in each of them (e.g. `CallsMachine` depends on `NO_CALLS` and vice-versa). @OP, you will have to split the classes to header/source files properly (forward-declare stuff in headers and `include` in source files), check the question that I linked. – SingerOfTheFall Oct 07 '15 at 10:35
  • @SingerOfTheFall You're right I didn't spot that – EdChum Oct 07 '15 at 10:44

0 Answers0