473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 2393
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
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
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
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
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
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
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
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
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
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.