Conflicting declaration of objects. Need Solution Please
The Light and Fan are two different vendor classes and they are not derived from any base class.
I am trying to implement the Command design pattern but with generic implementation, so it should work in future with any new vendor class like Door, Window etc without much change in client code. I have thought about a Factory method but it will not work because it needs a Base class.
It is not an assignment; I am trying to learn the design patterns.
Light *myobj;
Fan *myobj;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1){
Light *myobj = new Light();
}
if (choice ==2){
Fan *myobj = new Fan();
}
FlipUpCommand switchUp(*myobj);
Error:conflicting declaration 'Fan*myobj'
Re: Conflicting declaration of objects. Need Solution Please
You have four different declarations of myobj. What's wrong with a base class?
Re: Conflicting declaration of objects. Need Solution Please
I am assuming that I do not have the right to change the vendor class and each vendor is providing its own class of implementation.
Re: Conflicting declaration of objects. Need Solution Please
Give those "each vendor" class objects different names!
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
VictorN
Give those "each vendor" class objects different names!
Ok. then I have to put FlipUpCommand switchUp(*myobj); statement in each 'IF' which I do not want to do.
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
fsixteen
Ok. then I have to put FlipUpCommand switchUp(*myobj); statement in each 'IF' which I do not want to do.
Then you could do something like
Code:
void *myobj = NULL;
Light *myLight;
Fan *myFan;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1)
{
Light *myLight = new Light();
myobj = (void*)myLight;
}
if (choice ==2)
{
Fan *myFan = new Fan();
myobj = (void*)myFan;
}
FlipUpCommand switchUp(*myobj);
Note that in such a case your FlipUpCommand class had to know what type of object (Light or Fan) was actually passed in and cast the void* pointer to corresponding class.
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
VictorN
Then you could do something like
Code:
void *myobj = NULL;
Light *myLight;
Fan *myFan;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1)
{
Light *myLight = new Light();
myobj = (void*)myLight;
}
if (choice ==2)
{
Fan *myFan = new Fan();
myobj = (void*)myFan;
}
FlipUpCommand switchUp(*myobj);
Note that in such a case your FlipUpCommand class had to know what type of object (Light or Fan) was actually passed in and cast the void* pointer to corresponding class.
Error: `void*' is not a pointer-to-object type
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
fsixteen
Error: `void*' is not a pointer-to-object type
What line produces this error?
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
VictorN
What line produces this error?
FlipUpCommand switchUp(*myobj);
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
fsixteen
FlipUpCommand switchUp(*myobj);
How did you declare it (FlipUpCommand ctor)?
In the suggested design (if your Fan and Light classes do not have a common base class) it should be declared as
Code:
FlipUpCommand (void*);
Re: Conflicting declaration of objects. Need Solution Please
Please, edit your post adding Code tags around your code snippets!
Otherwise your code is absolutely unreadable! :(
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
VictorN
Please, edit your post adding Code tags around your code snippets!
Otherwise your code is absolutely unreadable! :(
trying code edit
Quote:
Code:
#include <iostream>
#include <typeinfo>
using namespace std;
/*the Command interface*/
class Command
{
public:
template <typename t>
virtual void execute(t obj)=0;
template <typename T>
friend bool operator ==( T left, T right );
};
template <typename T>
bool operator==( T left, T right ){
return (left == right);
}
/*Receiver class*/
class Light {
public:
Light() { }
void turnOn()
{
cout << "The light is on" << endl;
}
void turnOff()
{
cout << "The light is off" << endl;
}
};
class Fan {
public:
Fan() { }
void turnOn()
{
cout << "The Fan is on" << endl;
}
void turnOff()
{
cout << "The Fan is off" << endl;
}
};
/*the Command for turning on the light*/
class FlipUpCommand: public Command
{
public:
template <typename t>
FlipUpCommand(t myobject)
{
if (myobject == theLight){
theLight=myobject;
execute(theLight)
}
if (myobject = theFan){
theFan = myobject;
execute(theLight)
}
}
template <typename t>
virtual void execute(t obj)
{
obj.turnOn();
}
public:
Light theLight;
Fan theFan;
};
class FlipDownCommand: public Command
{
public:
template <typename t>
FlipUpCommand(t myobject)
{
if (myobject == theLight){
theLight=myobject;
execute(theLight)
}
if (myobject = theFan){
theFan = myobject;
execute(theLight)
}
}
template <typename t>
virtual void execute(t obj)
{
obj.turnOff();
}
public:
Light theLight;
Fan theFan;
};
class Switch {
public:
Switch(Command& flipUpCmd, Command& flipDownCmd)
:flipUpCommand(flipUpCmd),flipDownCommand(flipDownCmd)
{
}
void flipUp()
{
flipUpCommand.execute();
}
void flipDown()
{
flipDownCommand.execute();
}
public:
Command& flipUpCommand;
Command& flipDownCommand;
};
/*The test class or client*/
int main()
{
void *myobj = NULL;
Light *myLight;
Fan *myFan;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1)
{
Light *myLight = new Light();
myobj = (void*)myLight;
}
if (choice ==2)
{
Fan *myFan = new Fan();
myobj = (void*)myFan;
}
FlipUpCommand switchUp(*myobj);
FlipUpCommand switchUp(*myobj);
FlipDownCommand switchDown(*myobj);
Switch s(switchUp, switchDown);
cout<<" ON (1), OFF(2) :";
cin>>choice;
if(choice ==1){
s.flipUp();
}
if(choice==2){
s.flipDown();
}
int stop;
cin>>stop;
}
Re: Conflicting declaration of objects. Need Solution Please
Why the colors are not appearing in my code ?
Re: Conflicting declaration of objects. Need Solution Please
Quote:
Originally Posted by
fsixteen
Why the colors are not appearing in my code ?
You'd better have asked: "Why the code indentations are not appearing in my code ?" :mad:
And the answer would be very simple: just because you put unformatted code between the tags! :thumbd:
Note, that I didn't ask you to repost the code, just to edit the previous post adding code tags!
Re: Conflicting declaration of objects. Need Solution Please
How to solve this problem.
class FlipUpCommand: public Command
{
public:
template <typename t>
FlipUpCommand(t *obj)
{
}
};
FlipUpCommand switchUp(*myobj);
Error: void is not a pointer to object Type.
I believe I am doing something wrong with FlipUpCommand constructor