P
Pallav singh
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:n()
{ current->on(this); }
void Machine: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:ff(Machine * m)
{
cout<<"Going from ON to OFF";
m->setcurrentstate( new OFF() );
delete this;
}
int main()
{
void (Machine::*ptrs[] )() = { Machine:ff, Machine:n }; // Error
Point
Machine FSM;
int num;
while(1)
{ cout <<"Enter 0 / 1 : ";
cin >> num;
(FSM.*ptrs[num])();
}
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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:n()
{ current->on(this); }
void Machine: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:ff(Machine * m)
{
cout<<"Going from ON to OFF";
m->setcurrentstate( new OFF() );
delete this;
}
int main()
{
void (Machine::*ptrs[] )() = { Machine:ff, Machine:n }; // Error
Point
Machine FSM;
int num;
while(1)
{ cout <<"Enter 0 / 1 : ";
cin >> num;
(FSM.*ptrs[num])();
}
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++