-
January 18th, 2013, 06:28 AM
#1
How to create global object
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.
-
January 18th, 2013, 06:36 AM
#2
Re: How to create global object
Just define thebefore the
Victor Nijegorodov
-
January 18th, 2013, 06:55 AM
#3
Re: How to create global object
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'
-
January 18th, 2013, 08:06 AM
#4
Re: How to create global object
Don't you understand what "conflicting" means?
Victor Nijegorodov
-
January 18th, 2013, 08:32 AM
#5
Re: How to create global object
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?
-
January 18th, 2013, 09:00 AM
#6
Re: How to create global object
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.
-
January 18th, 2013, 09:05 AM
#7
Re: How to create global object
Originally Posted by fsixteen
...
For this reason I tired to define two objects of different classes with the same name.
Sorry, but it is NOT possible!
PS: you have to learn the basics of programming from the very begin!
Victor Nijegorodov
-
January 18th, 2013, 03:51 PM
#8
Re: How to create global object
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
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);
}
with switchUp defined something like switchUp(baseclass* choice)
-
January 18th, 2013, 04:17 PM
#9
Re: How to create global object
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);
-
January 21st, 2013, 05:04 AM
#10
Re: How to create global object
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.
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
|