1

This is an example of State machine design pattern .. i am facing some issue in it please explain and give solution for it .

This is the code:

#include <iostream>
using namespace std;
class Machine
{
  class State *current;
  public:
    Machine();
    void setCurrent(State *s)
    {
        current = s;
    }
    void on();
    void off();
};

class State
{
  public:
    virtual void on(Machine *m)
    {
        cout << "   already ON\n";
    }
    virtual void off(Machine *m)
    {
        cout << "   already OFF\n";
    }
};

void Machine::on()
{
  current->on(this);
}

void Machine::off()
{
  current->off(this);
}

class ON: public State
{
  public:
    ON()
    {
        cout << "   ON-ctor ";
    };
    ~ON()
    {
        cout << "   dtor-ON\n";
    };
    void off(Machine *m);
};

class OFF: public State
{
  public:
    OFF()
    {
        cout << "   OFF-ctor ";
    };
    ~OFF()
    {
        cout << "   dtor-OFF\n";
    };
    void on(Machine *m)
    {
        cout << "   going from OFF to ON";
        m->setCurrent(new ON());
        delete this;
    }
};

void ON::off(Machine *m)
{
  cout << "   going from ON to OFF";
  m->setCurrent(new OFF());
  delete this;
}

Machine::Machine()
{
  current = new OFF();
  cout << '\n';
}

int main()
{
  void(Machine:: *ptrs[])() = 
  {
    Machine::off, Machine::on
  };
  Machine fsm;
  int num;
  while (1)
  {
    cout << "Enter 0/1: ";
    cin >> num;
    (fsm. *ptrs[num])();
  }
}

This is the error:

prog.cpp: In function ‘int main()’:
prog.cpp:89: error: invalid use of non-static member function ‘void Machine::off()’
prog.cpp:89: error: invalid use of non-static member function ‘void Machine::on()’
prog.cpp:97: error: expected unqualified-id before ‘*’ token

3
  • 1
    There are so many bizarre things here that I don't know where to start. For one, listen to your compiler... You're trying to use the off and on methods like they're static. You need an instance of a Machine object in order to use them. Commented Nov 28, 2012 at 8:43
  • 1
    @ScoPi He has one: fsm. Its the initializer list and invoker that is broken (the latter literally by a single space). Commented Nov 28, 2012 at 8:47
  • My recommendation is just not to write C++ that looks like this. Commented Nov 28, 2012 at 9:57

1 Answer 1

6

There are two errors:

One, the address-of operator is mandatory for creating pointers to members. So they array initialisation should be:

void(Machine:: *ptrs[])() = 
{
  &Machine::off, &Machine::on
};

Two, the operator to dereference a pointer to member is .*. It's a single token, so no whitespace is allowed:

(fsm.*ptrs[num])();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.