-
January 22nd, 2013, 09:43 AM
#1
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'
-
January 22nd, 2013, 09:51 AM
#2
Re: Conflicting declaration of objects. Need Solution Please
You have four different declarations of myobj. What's wrong with a base class?
-
January 22nd, 2013, 09:59 AM
#3
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.
-
January 22nd, 2013, 01:13 PM
#4
Re: Conflicting declaration of objects. Need Solution Please
Give those "each vendor" class objects different names!
Victor Nijegorodov
-
January 23rd, 2013, 04:10 AM
#5
Re: Conflicting declaration of objects. Need Solution Please
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.
-
January 23rd, 2013, 04:20 AM
#6
Re: Conflicting declaration of objects. Need Solution Please
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.
Victor Nijegorodov
-
January 23rd, 2013, 04:32 AM
#7
Re: Conflicting declaration of objects. Need Solution Please
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
-
January 23rd, 2013, 04:38 AM
#8
Re: Conflicting declaration of objects. Need Solution Please
Originally Posted by fsixteen
Error: `void*' is not a pointer-to-object type
What line produces this error?
Victor Nijegorodov
-
January 23rd, 2013, 04:44 AM
#9
Re: Conflicting declaration of objects. Need Solution Please
Originally Posted by VictorN
What line produces this error?
FlipUpCommand switchUp(*myobj);
-
January 23rd, 2013, 04:50 AM
#10
Re: Conflicting declaration of objects. Need Solution Please
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*);
Victor Nijegorodov
-
January 23rd, 2013, 05:21 AM
#11
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!
Victor Nijegorodov
-
January 23rd, 2013, 05:46 AM
#12
Re: Conflicting declaration of objects. Need Solution Please
Originally Posted by VictorN
Please, edit your post adding Code tags around your code snippets!
Otherwise your code is absolutely unreadable!
trying code edit
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;
}
Last edited by fsixteen; January 23rd, 2013 at 06:22 AM.
Reason: to edit the previous post adding code tags
-
January 23rd, 2013, 05:50 AM
#13
Re: Conflicting declaration of objects. Need Solution Please
Why the colors are not appearing in my code ?
-
January 23rd, 2013, 06:02 AM
#14
-
January 23rd, 2013, 07:50 AM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|