473,513 Members | 2,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2395
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
7192
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
7
2885
by: Peter Verburgh | last post by:
Hello, Is there an easy way in C# to save a component (control) state (example. save all the property values from a button control) to a file (xml) ? Kind regards; Peter.
13
3401
by: ScottM | last post by:
I have run into a problem generating the class file via the WSDL utility. I have a WSDL file that was generated by XMLSpy and is able to be read by the Java code utility, but I get the following...
35
3739
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
4
14705
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns – State Often computer software operates based on a condition called a state. These states traditionally have been implemented using a switch statement. The cases of the switch...
5
4020
by: gordon.is.a.moron | last post by:
Hello, I'm implementing a State Pattern in C++, based on the example in the GoF book. However the example they give only shows a single transition to another state. In my program I have a choice...
6
2152
by: pcrepieux | last post by:
Hi, I recently meet a problem while "playing" with the state pattern. I was wondering if each of the member function dedicated to handle events open(), close(), ack() could be change to...
118
4530
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
3
270
by: Pallav singh | last post by:
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 ...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.