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

    about overloading function

    Code:
    #include<iostream>
    using namespace std;
    
    void f1()                  {cout << "overload def#1 of f1()" << endl;} // def#1
    void f1(int     x)         {cout << "overload def#2 of f1()" << endl;} // def#2
    void f1(double  x)         {cout << "overload def#3 of f1()" << endl;} // def#3
    void f1(double  x, char y) {cout << "overload def#4 of f1()" << endl;} // def#4
    void f1(int     x, char y) {cout << "overload def#5 of f1()" << endl;} // def#5
    void f1(double &x)         {cout << "overload def#6 of f1()" << endl;} // def#6
    
    int main()
    {
      /*1.*/ f1(2);
      /*2.*/ f1();
      /*3.*/ f1(2.5, 'A');
      /*4.*/ f1(2,   'A');
    
      double a = 2.5;
    
      /*5.*/ f1(2.5);
      /*6.*/ f1(a);
      return 0;
    } // end main()
    I can't understand why function call 6 can't call def#3.isn't their type the same?

    and if comment the function call at 6 i give default value to parameter x of def#2 ,it will be error function call 2.

    can someone explain to me how is it ambiguous?. or give me the chapter name so i can go re-study.
    at least give me a hint before i got more misunderstanding.

    sorry for my english.

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

    Re: about overloading function

    It can't tell if you want to call 3 or 6.

  3. #3
    Join Date
    Jan 2012
    Posts
    3

    Re: about overloading function

    isn't it reference passing?


    Code:
    #include<iostream>
    using namespace std;
    
    void f1()                  {cout << "overload def#1 of f1()" << endl;} // def#1
    void f1(int     x=3)         {cout << "overload def#2 of f1()" << endl;} // def#2
    void f1(double  x)         {cout << "overload def#3 of f1()" << endl;} // def#3
    void f1(double  x, char y) {cout << "overload def#4 of f1()" << endl;} // def#4
    void f1(int     x, char y) {cout << "overload def#5 of f1()" << endl;} // def#5
    void f1(double &x)         {cout << "overload def#6 of f1()" << endl;} // def#6
    
    int main()
    {
      /*1.*/ f1(2);    
      /*2.*/ f1();  /*<<<<this got error*/
      /*3.*/ f1(2.5, 'A');
      /*4.*/ f1(2,   'A');
    
      double a = 2.5;
    
      /*5.*/ f1(2.5);
    
      return 0;
    } // end main()
    this got error.it say ambiguous. why f1() call def#1.

    thanks for answer

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: about overloading function

    I can't understand why function call 6 can't call def#3.isn't their type the same?
    When overloaded function is met, compiler deduces the closest matching variant from the function call. Calling like f1(a) matches to both f1(double) and f1(double&), so there's an ambiguity which cannot be solved by compiler.

    To solve this, you need to signify which exact f1 version you need.
    Code:
      double a = 2.5;
    
      /*5.*/ f1(2.5);
      /*6.*/ ((void (*)(double &))f1)(a);
    Or better you never introduce such ambiguous overloaded function prototypes.
    Best regards,
    Igor

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: about overloading function

    this got error.it say ambiguous. why f1() call def#1.
    Code:
    void f1();
    void f1(int x=3);
    //...
    f1();  // which one will be called?
    Both of the f1() functions can be called with no arguments. That's why it is ambiguous.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2012
    Posts
    3

    Re: about overloading function

    thank you all of you .

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
  •  





Click Here to Expand Forum to Full Width

Featured