473,672 Members | 2,603 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<iostre am.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<<"Destruc tor invoked \n ";}
~ON() {cout<<"Constru ctor invoked \n ";}
void off(Machine * m);
};

class OFF : public state
{
public :
OFF() {cout<<"Destruc tor invoked \n ";}
~OFF() {cout<<"Constru ctor invoked \n ";}
void on(Machine * m)
{cout<<"Going from OFF to ON";
m->setcurrentstat e( new ON() );
delete this;
}
};

Machine::Machin e()
{
current = new OFF();
cout<<"Machine constructor Called "<<endl;
}
void ON::off(Machine * m)
{
cout<<"Going from ON to OFF";
m->setcurrentstat e( 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 2412
Pallav singh <si**********@g mail.comwrote:
int main()
{

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

"Pallav singh" <si**********@g mail.coma écrit dans le message de news:
2c************* *************** **...legroup s.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<iostre am.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<<"Destruc tor invoked \n ";}
~ON() {cout<<"Constru ctor invoked \n ";}
void off(Machine * m);
};

class OFF : public state
{
public :
OFF() {cout<<"Destruc tor invoked \n ";}
~OFF() {cout<<"Constru ctor invoked \n ";}
void on(Machine * m)
{cout<<"Going from OFF to ON";
m->setcurrentstat e( new ON() );
delete this;
}
};

Machine::Machin e()
{
current = new OFF();
cout<<"Machine constructor Called "<<endl;
}
void ON::off(Machine * m)
{
cout<<"Going from ON to OFF";
m->setcurrentstat e( 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************ *************** *******@w34g200 0prm.googlegrou ps.com>,
Pallav singh <si**********@g mail.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<iostre am.h>
(From comeau)
<iostream.his not a Standard header, use <iostreaminstea d.
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<<"Destruc tor invoked \n ";}
~ON() {cout<<"Constru ctor invoked \n ";}
void off(Machine * m);
};

class OFF : public state
{
public :
OFF() {cout<<"Destruc tor invoked \n ";}
~OFF() {cout<<"Constru ctor invoked \n ";}
void on(Machine * m)
{cout<<"Going from OFF to ON";
m->setcurrentstat e( new ON() );
delete this;
}
};

Machine::Machin e()
{
current = new OFF();
cout<<"Machine constructor Called "<<endl;
}
void ON::off(Machine * m)
{
cout<<"Going from ON to OFF";
m->setcurrentstat e( 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
7218
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
2895
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
3424
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 error from the WSDL utility. Error: Unable to import binding 'REMSOAPHttpsBinding' from namespace 'http://www.fnfis.com/services/Services-Common/REM/20040109/v1.0'. - The operation 'REMCorporationBPOOrder' on portType 'REMInterface' from...
35
3770
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 gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
4
14722
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 represent the various states. The example below might be used to read a disc file int state = 1; switch (state) { case 1: //open the file
5
4030
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 of states depending on the input. So, I was going to give each concrete State a small Map of inputs to States and then find out what transition to make. However I was wondering if this is a sort of mix between State Table and State Pattern,...
6
2181
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 something like process(openEv& ev), process(closeEv& ev), ... no problem with this point. Going further in this way, i thought that the process member function would be a great candidate for a template. Hum ... it is not. The process function have to be...
118
4620
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
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8608
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8680
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7446
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.