469,962 Members | 2,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,962 developers. It's quick & easy.

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;
}

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++
May 31 '08 #1
3 2128
Pallav singh <si**********@gmail.comwrote:
int main()
{

void (Machine::*ptrs[] )() = { Machine::off, Machine::on }; // Error
Point
void (Machine::*ptrs[])() = { &Machine::off, &Machine::on };
May 31 '08 #2

"Pallav singh" <si**********@gmail.coma crit dans le message de news:
2c**********************************...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...
May 31 '08 #3
In article
<2c**********************************@w34g2000prm. googlegroups.com>,
Pallav singh <si**********@gmail.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<iostream.h>
(From comeau)
<iostream.his not a Standard header, use <iostreaminstead.
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<<"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 }; // 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;
^
}
May 31 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Peter Verburgh | last post: by
13 posts views Thread by ScottM | last post: by
35 posts views Thread by jeffc226 | last post: by
5 posts views Thread by gordon.is.a.moron | last post: by
6 posts views Thread by pcrepieux | last post: by
118 posts views Thread by Chuck Cheeze | last post: by
3 posts views Thread by Pallav singh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.