473,772 Members | 2,965 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 2417
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
7238
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
2899
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
3436
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
3807
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
14729
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
4040
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
2185
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
4696
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
9620
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
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...
0
6715
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.