简体   繁体   中英

State design pattern - Compilation error


I get three errors while i try to compile this program.

I am expecting the following output

OFF-ctor
Enter 0/1: 0 already OFF
Enter 0/1: 1
going from OFF to ON ON-ctor dtor-OFF
Enter 0/1: 1
already ON
Enter 0/1: 0
going from ON to OFF OFF-ctor dtor-ON
Enter 0/1: 1
going from OFF to ON ON-ctor dtor-OFF
Enter 0/1: 0
going from ON to OFF OFF-ctor dtor-ON
Enter 0/1: 0
already OFF Enter 0/1:

Following is the program

    #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//Error2->invalid use of non-static member function 'void Machine::off()'
//Error3->invalid use of non-static member function 'void Machine::on()'
      };
      Machine fsm;
      int num;
      while (1)
      {
        cout << "Enter 0/1: ";
        cin >> num;
        (fsm. *ptrs[num])(); //Error1->expected unqualified-id before '*' token
      }
    }

The code was taken from sourcemaking.com under state design pattern. I ran it in eclipse and linux g++. Kindly help.

To get a pointer to a member function, you need to use an & (even though it's optional for getting a pointer to a non-member function): &Machine::off, &Machine::on

For the other, you need to realize that .* is a single token, so you need to remove the space between the two characters: (fsm.*ptrs[num])();

void (Machine::*ptrs[])() =
  {
    &Machine::off,  // note: explicit &
    &Machine::on
  };
Machine fsm;
int num;
while (1)
{
    cout << "Enter 0/1: ";
    cin >> num;
    (fsm.*ptrs[num])();  // note: no space between . and *
}

That still leaves the following warnings:

try.cc:19:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc:23:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc: In member function 'virtual void OFF::on(Machine*)':
try.cc:68:20: warning: deleting object of polymorphic class type 'OFF' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
try.cc: In member function 'virtual void ON::off(Machine*)':
try.cc:76:14: warning: deleting object of polymorphic class type 'ON' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM