Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Error while writing State Design Pattern Code (http://www.velocityreviews.com/forums/t617793-error-while-writing-state-design-pattern-code.html)

Pallav singh 05-31-2008 11:54 AM

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

void Machine::off()
{ 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::off(Machine * m)
{
cout<<"Going from ON to OFF";
m->setcurrentstate( 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;
}

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

Eric Pruneau 05-31-2008 01:43 PM

Re: Error while writing State Design Pattern Code
 

"Pallav singh" <singh.pallav@gmail.com> a écrit dans le message de news:
2c1ee6ab-5e22-48e5-8a75-f6c6d438a841...oglegroups.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<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::on()
> { current->on(this); }
>
> void Machine::off()
> { 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::off(Machine * m)
> {
> cout<<"Going from ON to OFF";
> m->setcurrentstate( 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...




All times are GMT. The time now is 04:17 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57