Error while writing State Design Pattern Code

Discussion in 'C++' started by Pallav singh, May 31, 2008.

  1. Pallav singh

    Pallav singh Guest

    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<iostream.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::eek:n()
    { current->on(this); }

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

    class ON : public state
    {
    public :
    ON() {cout<<"Destructor invoked \n ";}
    ~ON() {cout<<"Constructor invoked \n ";}
    void off(Machine * m);
    };

    class OFF : public state
    {
    public :
    OFF() {cout<<"Destructor invoked \n ";}
    ~OFF() {cout<<"Constructor invoked \n ";}
    void on(Machine * m)
    {cout<<"Going from OFF to ON";
    m->setcurrentstate( new ON() );
    delete this;
    }
    };



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


    void ON::eek:ff(Machine * m)
    {
    cout<<"Going from ON to OFF";
    m->setcurrentstate( new OFF() );
    delete this;
    }




    int main()
    {

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

    return 0;
    }

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     
    Pallav singh, May 31, 2008
    #1
    1. Advertising

  2. Pallav singh

    Eric Pruneau Guest

    "Pallav singh" <> a écrit dans le message de news:
    ...
    > 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<iostream.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::eek:n()
    > { current->on(this); }
    >
    > void Machine::eek:ff()
    > { current->off(this); }
    >
    > class ON : public state
    > {
    > public :
    > ON() {cout<<"Destructor invoked \n ";}
    > ~ON() {cout<<"Constructor invoked \n ";}
    > void off(Machine * m);
    > };
    >
    > class OFF : public state
    > {
    > public :
    > OFF() {cout<<"Destructor invoked \n ";}
    > ~OFF() {cout<<"Constructor invoked \n ";}
    > void on(Machine * m)
    > {cout<<"Going from OFF to ON";
    > m->setcurrentstate( new ON() );
    > delete this;
    > }
    > };
    >
    >
    >
    > Machine::Machine()
    > {
    > current = new OFF();
    > cout<<"Machine constructor Called "<<endl;
    > }
    >
    >
    > void ON::eek:ff(Machine * m)
    > {
    > cout<<"Going from ON to OFF";
    > m->setcurrentstate( new OFF() );
    > delete this;
    > }
    >
    >
    >
    >
    > int main()
    > {
    >
    > void (Machine::*ptrs[] )() = { Machine::eek:ff, Machine::eek:n }; // 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::eek:ff, Machine::eek:n }; // Error
    Point

    for

    void (Machine::*ptrs[] )() = { Machine::eek:ff, Machine::eek:n }; // Error Point

    I guess it is a typo error...
     
    Eric Pruneau, May 31, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    709
    bruno modulix
    Aug 11, 2005
  2. sunny
    Replies:
    1
    Views:
    468
    Salt_Peter
    Dec 7, 2006
  3. Pallav singh
    Replies:
    0
    Views:
    366
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    405
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    454
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page