CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2013
    Posts
    13

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: How to create global object

    Just define the
    Code:
    Light *myobj;
    before the
    Code:
    if (choice ==1)
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2013
    Posts
    13

    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'

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: How to create global object

    Don't you understand what "conflicting" means?
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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?

  6. #6
    Join Date
    Jan 2013
    Posts
    13

    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.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: How to create global object

    Quote Originally Posted by fsixteen View Post
    ...
    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

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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);

  10. #10
    Join Date
    Jan 2013
    Posts
    13

    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
  •  





Click Here to Expand Forum to Full Width

Featured