Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Error while writing State Design Pattern Code

Reply
Thread Tools

Error while writing State Design Pattern Code

 
 
Pallav singh
Guest
Posts: n/a
 
      05-31-2008
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;
}

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++
 
Reply With Quote
 
 
 
 
Eric Pruneau
Guest
Posts: n/a
 
      05-31-2008

"Pallav singh" <(E-Mail Removed)> 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: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;
> }
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++


Well I don't get any errors while compiling it with intel c++ compiler...
Except that I changed :

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

for

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

I guess it is a typo error...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 1 01-22-2012 10:48 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:26 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:25 PM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
State query in State Pattern kk_oop@yahoo.com Java 8 08-11-2005 05:03 PM



Advertisments