1


このプログラムをコンパイルしようとすると、3つのエラーが発生します。

私は次の出力を期待しています

OFF-ctor
入力0/1:0すでにオフ
入力0/1:1
オフからオンに移行ON-ctor dtor-OFF
入力0/1:1
すでにオン
入力0/1:0
オンからオフに移行OFF-ctor dtor-ON
0/1:1
をOFFからONに入力ON-ctor dtor-OFF
0/1:0
をONからOFFに入力OFF-ctor dtor-ON
0/1:0
を入力すでにOFF 0/1を入力:

以下はプログラムです

    #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
      }
    }

コードは、状態デザインパターンの下でsourcemaking.comから取得されました。私はそれをEclipseとLinuxg++で実行しました。親切に助けてください。

4

2 に答える 2

3

&メンバー関数へのポインターを取得するには、 (非メンバー関数へのポインターを取得するためのオプションですが)を使用する必要があります。&Machine::off, &Machine::on

もう.*1つは、それが単一のトークンであることを認識する必要があるため、2つの文字の間のスペースを削除する必要があります。(fsm.*ptrs[num])();

于 2013-02-26T06:42:46.480 に答える
2
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 *
}

それでも次の警告が残ります。

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]
于 2013-02-26T06:44:28.730 に答える