Conflicting declaration of objects. Need Solution Please
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Conflicting declaration of objects. Need Solution Please

  1. #1
    Join Date
    Jan 2013
    Posts
    13

    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'

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,231

    Re: Conflicting declaration of objects. Need Solution Please

    You have four different declarations of myobj. What's wrong with a base class?

  3. #3
    Join Date
    Jan 2013
    Posts
    13

    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.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

    Re: Conflicting declaration of objects. Need Solution Please

    Give those "each vendor" class objects different names!
    Victor Nijegorodov

  5. #5
    Join Date
    Jan 2013
    Posts
    13

    Re: Conflicting declaration of objects. Need Solution Please

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

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

    Re: Conflicting declaration of objects. Need Solution Please

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

  7. #7
    Join Date
    Jan 2013
    Posts
    13

    Re: Conflicting declaration of objects. Need Solution Please

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

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

    Re: Conflicting declaration of objects. Need Solution Please

    Quote Originally Posted by fsixteen View Post
    Error: `void*' is not a pointer-to-object type
    What line produces this error?
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2013
    Posts
    13

    Re: Conflicting declaration of objects. Need Solution Please

    Quote Originally Posted by VictorN View Post
    What line produces this error?
    FlipUpCommand switchUp(*myobj);

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

    Re: Conflicting declaration of objects. Need Solution Please

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

  11. #11
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

    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

  12. #12
    Join Date
    Jan 2013
    Posts
    13

    Re: Conflicting declaration of objects. Need Solution Please

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

  13. #13
    Join Date
    Jan 2013
    Posts
    13

    Re: Conflicting declaration of objects. Need Solution Please

    Why the colors are not appearing in my code ?

  14. #14
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

    Re: Conflicting declaration of objects. Need Solution Please

    Quote Originally Posted by fsixteen View Post
    Why the colors are not appearing in my code ?
    You'd better have asked: "Why the code indentations are not appearing in my code ?"
    And the answer would be very simple: just because you put unformatted code between the tags!

    Note, that I didn't ask you to repost the code, just to edit the previous post adding code tags!
    Victor Nijegorodov

  15. #15
    Join Date
    Jan 2013
    Posts
    13

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center