Error while writing State Design Pattern Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pallav singh

    Error while writing State Design Pattern Code

    Hi All

    i am getting Error while writing following code for state design
    Pattern
    kindly let me know How to Correct this Error ??

    Thanks
    Pallav Singh

    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++

    #include<iostre am.h>
    using namespace std;

    class state;

    class Machine
    {
    class state * current;
    public :
    Machine();

    void setcurrentstate (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<<"Destruc tor invoked \n ";}
    ~ON() {cout<<"Constru ctor invoked \n ";}
    void off(Machine * m);
    };

    class OFF : public state
    {
    public :
    OFF() {cout<<"Destruc tor invoked \n ";}
    ~OFF() {cout<<"Constru ctor invoked \n ";}
    void on(Machine * m)
    {cout<<"Going from OFF to ON";
    m->setcurrentstat e( new ON() );
    delete this;
    }
    };



    Machine::Machin e()
    {
    current = new OFF();
    cout<<"Machine constructor Called "<<endl;
    }


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




    int main()
    {

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

    return 0;
    }

    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++
  • Daniel T.

    #2
    Re: Error while writing State Design Pattern Code

    Pallav singh <singh.pallav@g mail.comwrote:
    int main()
    {
    >
    void (Machine::*ptrs[] )() = { Machine::off, Machine::on }; // Error
    Point
    void (Machine::*ptrs[])() = { &Machine::of f, &Machine::on };

    Comment

    • Eric Pruneau

      #3
      Re: Error while writing State Design Pattern Code


      "Pallav singh" <singh.pallav@g mail.coma écrit dans le message de news:
      2c1ee6ab-5e22-48e5-8a75-f6c6d438a841...l egroups.com...
      Hi All
      >
      i am getting Error while writing following code for state design
      Pattern
      kindly let me know How to Correct this Error ??
      >
      Thanks
      Pallav Singh
      >
      +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++
      >
      #include<iostre am.h>
      using namespace std;
      >
      class state;
      >
      class Machine
      {
      class state * current;
      public :
      Machine();
      >
      void setcurrentstate (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<<"Destruc tor invoked \n ";}
      ~ON() {cout<<"Constru ctor invoked \n ";}
      void off(Machine * m);
      };
      >
      class OFF : public state
      {
      public :
      OFF() {cout<<"Destruc tor invoked \n ";}
      ~OFF() {cout<<"Constru ctor invoked \n ";}
      void on(Machine * m)
      {cout<<"Going from OFF to ON";
      m->setcurrentstat e( new ON() );
      delete this;
      }
      };
      >
      >
      >
      Machine::Machin e()
      {
      current = new OFF();
      cout<<"Machine constructor Called "<<endl;
      }
      >
      >
      void ON::off(Machine * m)
      {
      cout<<"Going from ON to OFF";
      m->setcurrentstat e( new OFF() );
      delete this;
      }
      >
      >
      >
      >
      int main()
      {
      >
      void (Machine::*ptrs[] )() = { Machine::off, Machine::on }; // Error
      Point
      Machine FSM;
      int num;
      while(1)
      { cout <<"Enter 0 / 1 : ";
      cin >num;
      (FSM.*ptrs[num])();
      }
      >
      return 0;
      }
      >
      +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++
      Well I don't get any errors while compiling it with intel c++ compiler...
      Except that I changed :

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

      for

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

      I guess it is a typo error...


      Comment

      • Daniel T.

        #4
        Re: Error while writing State Design Pattern Code

        In article
        <2c1ee6ab-5e22-48e5-8a75-f6c6d438a841@w3 4g2000prm.googl egroups.com>,
        Pallav singh <singh.pallav@g mail.comwrote:
        Hi All
        >
        i am getting Error while writing following code for state design
        Pattern
        kindly let me know How to Correct this Error ??
        >
        Thanks
        Pallav Singh
        Results from compiling in: http://www.comeaucomputing.com/tryitout and
        my own observations...
        +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++
        >
        #include<iostre am.h>
        (From comeau)
        <iostream.his not a Standard header, use <iostreaminstea d.
        using namespace std;
        >
        class state;
        >
        class Machine
        {
        class state * current;
        keyword 'class' unnecessary above.
        public :
        Machine();
        >
        void setcurrentstate (state * s)
        { current = s ; }
        >
        void on();
        void off();
        };
        >
        class state
        {
        public :
        virtual void on(Machine * m)
        {cout<< " Already On \n"; }
        Above, and in general, your use of whitespace is inconsistent,
        especially inside quotes.
        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<<"Destruc tor invoked \n ";}
        ~ON() {cout<<"Constru ctor invoked \n ";}
        void off(Machine * m);
        };
        >
        class OFF : public state
        {
        public :
        OFF() {cout<<"Destruc tor invoked \n ";}
        ~OFF() {cout<<"Constru ctor invoked \n ";}
        void on(Machine * m)
        {cout<<"Going from OFF to ON";
        m->setcurrentstat e( new ON() );
        delete this;
        }
        };
        >
        >
        >
        Machine::Machin e()
        {
        current = new OFF();
        cout<<"Machine constructor Called "<<endl;
        }
        >
        >
        void ON::off(Machine * m)
        {
        cout<<"Going from ON to OFF";
        m->setcurrentstat e( new OFF() );
        delete this;
        }
        >
        >
        >
        >
        int main()
        {
        >
        void (Machine::*ptrs[] )() = { Machine::off, Machine::on }; // ErrorPoint
        (From comeau)
        error: nonstandard form for taking the address of a member function
        void (Machine::*ptrs[] )() = { Machine::off, Machine::on };
        ^

        (From comeau)
        error: nonstandard form for taking the address of a member function
        void (Machine::*ptrs[] )() = { Machine::off, Machine::on };
        ^
        Machine FSM;
        int num;
        while(1)
        { cout <<"Enter 0 / 1 : ";
        cin >num;
        (FSM.*ptrs[num])();
        }
        >
        return 0;
        (From comeau)
        warning: statement is unreachable
        return 0;
        ^
        }

        Comment

        Working...