if (choice ==1){
Light *myobj = new Light();
}
FlipUpCommand switchUp(*myobj);
Error: `myobj' undeclared (first use this function)
Please advice how to solve this problem without changing the Light class.
Printable View
if (choice ==1){
Light *myobj = new Light();
}
FlipUpCommand switchUp(*myobj);
Error: `myobj' undeclared (first use this function)
Please advice how to solve this problem without changing the Light class.
Just define thebefore theCode:Light *myobj;
Code:if (choice ==1)
Hi,
How to solve the below mentioned problem ?
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'
Don't you understand what "conflicting" means? :confused:
You are first defining myobj as a pointer to the class Light. You are then defining myobj as a pointer to the class Fan. You now have two definitions for myobj which are different - which the language/compiler does not allow. You are trying to have myobj as either a pointer to Light or a pointer to Fan depending upon choice and then passing myobj as a parameter to switchUp. Unless Light/Fan are derived from the same base class and you are trying an example for polymorphism then I suspect your design is wrong. What is the defintion of switchUp? and what are you trying to achieve?
I am trying to create an object at user choice and then passing that object to the
FlipUpCommand switchUp(*myobj);
If user select Light then it creates Light object, if user select Fan then create fan object and pass it.
I do not want to put FlipUpCommand switchUp(*myobj); under IF statement because I want it
independent from the object selection. What ever is selected just pass it.
For this reason I tired to define two objects of different classes with the same name.
The only way you can create different objects and pass either to switchUp is if the Light and Fan classes are derived from the same base class with switchUp taking as argument a pointer (or a reference) to the base class. Then you can pass a pointer to either the Light or Fan class to switchUp. This is called polymorphism and usually also involves virtual methods defined in the base class. But I think this is way beyond your current level of knowledge. Is this part of a homework assignment? What does the assignment actually ask you to do because I don't think it will ask you to create two objects of different classes with the same name - as even under polymorphism you still can't have two objects within the same scope with the same name.
If you are using polymorphism with a base class and derived classes then the code would look something like this
with switchUp defined something like switchUp(baseclass* choice)Code:int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1){
Light *mylight = new Light();
FlipUpCommand switchUp(mylight);
}
if (choice ==2){
Fan *myfan = new Fan();
FlipUpCommand switchUp(myfan);
}
Or if you really want it as global object, then suggest something like
Code:int choice;
Light *mylight = new Light();
Fan *myfan = new Fan();
Base *myclass;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1) {
myclass = mylight;
} else {
if (choice ==2) {
myclass = myfan;
} else {
myclass = NULL;
}
}
FlipUpCommand switchUp(myclass);
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.