simple issue on function overloading in c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: simple issue on function overloading in c++

  1. #1
    Join Date
    Apr 2014
    Posts
    4

    simple issue on function overloading in c++

    I'm really confused and hence putting this very simple on function overloading. Below is the sample code

    struct X
    {
    };

    struct Y
    {
    }

    struct Z
    {
    };

    X x1;
    Y y1;
    Z z1;

    set_data(X *m)
    {
    // need to initialize Z and Y from this funciton by getting reference to an instance
    // create instance of X,Y,Z

    set_data(&y1);
    set_data(&z1);
    }

    set_data(Y *m)
    {
    // to initialize Y struct
    }

    set_data(Z *m)
    {
    // to initialize Z struct
    }

    But i do so it gives error "error C2664: 'set_data' : cannot convert parameter 1 from 'X *' to 'Y *'
    Please suggest me where i'm wrong in above code

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: simple issue on function overloading in c++

    Quote Originally Posted by manishak View Post
    Below is the sample code

    Code:
    struct X
    {    
    };
    
    struct Y
    {    
    } <- semicolon is missing
    
    struct Z
    {    
    };
    
    X x1;
    Y y1;
    Z z1;
    
    set_data(X *m)
    {
        // need to initialize Z and Y from this funciton by getting reference to an instance
        // create instance of X,Y,Z
    
        set_data(&y1);
        set_data(&z1);
    }
    
    set_data(Y *m)
    {
        // to initialize Y struct
    }
    
    set_data(Z *m)
    {
         // to initialize Z struct
    }
    But i do so it gives error "error C2664: 'set_data' : cannot convert parameter 1 from 'X *' to 'Y *'
    Please suggest me where i'm wrong in above code
    First you have to use Code tags while posting code snippets. Otherwise your code is very hard to read/understand.
    Second you missed the semicolon after struct Ydefinition
    Third you missed to declare the prototipes of set_data functions.
    Fourth you missed to define the function types.
    So the corrected code should look like
    Code:
    struct X
    {    
    };
    
    struct Y
    {    
    };
    
    struct Z
    {    
    };
    void set_data(X *m);
    void set_data(Y *m);
    void set_data(Z *m);
    
    X x1;
    Y y1;
    Z z1;
    
    void set_data(X *m)
    {
        // need to initialize Z and Y from this funciton by getting reference to an instance
        // create instance of X,Y,Z
    
        set_data(&y1);
        set_data(&z1);
    }
    
    void set_data(Y *m)
    {
        // to initialize Y struct
    }
    
    void set_data(Z *m)
    {
         // to initialize Z struct
    }
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2014
    Posts
    4

    Red face Re: simple issue on function overloading in c++

    really thanks.. i forgot forward declaration of function..

Tags for this Thread

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