Like Tree10Likes
  • 2 Post By jimblumberg
  • 2 Post By Salem
  • 1 Post By manasij7479
  • 1 Post By Elysia
  • 4 Post By grumpy

function overloading error...

This is a discussion on function overloading error... within the C++ Programming forums, part of the General Programming Boards category; I have the following code Code: //illustrates overloading functions #include <iostream> using namespace std; class rational{ private: long a, q; ...

  1. #1
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88

    function overloading error...

    I have the following code
    Code:
    //illustrates overloading functions
    #include <iostream>
    using namespace std;
    
    class rational{
        
        private:
            long a, q;
            enum { BIG = 100};
    
        public:
            rational(int n = 0) : a(n), q(1) {}
        
            rational(int i, int j) : a(i), q(j) {}
    
            rational(double r) : a(static_cast<long> (r * BIG) ), q(BIG) {}
    
            void print() const { cout << a << "/" << q; }
        
            operator double() { return static_cast<double>(a) / q; }
    };
    
    //functions
    inline int greater(int i, int j){
        return ( i > j ? i : j);
    }
    
    inline double greater(double x, double y){
        return (x > y ? x : y); 
    }
    
    inline rational greater(rational w, rational z){
        return (w > z ? w : z); 
    }
    
    int main(void){
    
        int i = 10, j = 5;
         float x = 7.0;
        double y = 14.5;
        rational w(10), z(3.5), zmax;
    
        cout <<"\ngreater(" << i << "," << j << ") = " << greater(i,j);
        cout <<"\ngreater(" << x << "," << y << ") = " << greater(x,y);
        cout <<"\ngreater(" << i << ","; 
        z.print();
        cout <<") = " << greater(static_cast<rational>(i), z);
        zmax = greater(w,z);
        cout <<"\ngreater(";
        w.print();
        cout << ", ";
        z.print();
        cout << ") = ";
        zmax.print();
        cout << endl;
    
        return 0;
    }

    and getting the following errors.
    Code:
    $ g++ rational.cpp -o rational
    rational.cpp: In function ‘int main()’:
    rational.cpp:43:52: error: reference to ‘greater’ is ambiguous
    rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
    rational.cpp:28:15: error:                 double greater(double, double)
    rational.cpp:24:12: error:                 int greater(int, int)
    In file included from /usr/include/c++/4.7/string:50:0,
                     from /usr/include/c++/4.7/bits/locale_classes.h:42,
                     from /usr/include/c++/4.7/bits/ios_base.h:43,
                     from /usr/include/c++/4.7/ios:43,
                     from /usr/include/c++/4.7/ostream:40,
                     from /usr/include/c++/4.7/iostream:40,
                     from rational.cpp:2:
    /usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
    rational.cpp:44:52: error: reference to ‘greater’ is ambiguous
    rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
    rational.cpp:28:15: error:                 double greater(double, double)
    rational.cpp:24:12: error:                 int greater(int, int)
    In file included from /usr/include/c++/4.7/string:50:0,
                     from /usr/include/c++/4.7/bits/locale_classes.h:42,
                     from /usr/include/c++/4.7/bits/ios_base.h:43,
                     from /usr/include/c++/4.7/ios:43,
                     from /usr/include/c++/4.7/ostream:40,
                     from /usr/include/c++/4.7/iostream:40,
                     from rational.cpp:2:
    /usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
    rational.cpp:47:19: error: reference to ‘greater’ is ambiguous
    rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
    rational.cpp:28:15: error:                 double greater(double, double)
    rational.cpp:24:12: error:                 int greater(int, int)
    In file included from /usr/include/c++/4.7/string:50:0,
                     from /usr/include/c++/4.7/bits/locale_classes.h:42,
                     from /usr/include/c++/4.7/bits/ios_base.h:43,
                     from /usr/include/c++/4.7/ios:43,
                     from /usr/include/c++/4.7/ostream:40,
                     from /usr/include/c++/4.7/iostream:40,
                     from rational.cpp:2:
    /usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
    rational.cpp:48:9: error: reference to ‘greater’ is ambiguous
    rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
    rational.cpp:28:15: error:                 double greater(double, double)
    rational.cpp:24:12: error:                 int greater(int, int)
    In file included from /usr/include/c++/4.7/string:50:0,
                     from /usr/include/c++/4.7/bits/locale_classes.h:42,
                     from /usr/include/c++/4.7/bits/ios_base.h:43,
                     from /usr/include/c++/4.7/ios:43,
                     from /usr/include/c++/4.7/ostream:40,
                     from /usr/include/c++/4.7/iostream:40,
                     from rational.cpp:2:
    /usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
    Seems to be saying I can't call the greater function? Just learing about function overloading so any help would be appreciated.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    just replace
    Code:
    using namespace std;
    with
    Code:
    using  std::cout;
    using  std::endl;
    and the compiler will know what to call.
    It's never a good idea to give your own functions the same name like standard functions.
    Kurt

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    This is why the using namespace std; clause is not recommended. You could also put your greater() functions into your own namespace then properly scope your namespace using the scope resolution operator::

    Code:
    namespace hexDump
    {
    //functions
       inline int greater(int i, int j) {
          return ( i > j ? i : j);
       }
    
       inline double greater(double x, double y) {
          return (x > y ? x : y);
       }
    
       inline rational greater(rational w, rational z) {
          return (w > z ? w : z);
       }
    
    } // end of namespace
    Then when using your greater() functions:

    Code:
    zmax = hexDump::greater(w,z);
    Of course you could always rename your functions to something else as well.

    Jim
    Salem and Elkvis like this.

  4. #4
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Excellent thanks guys. So what would u call this? A scoping or namespace error?

    Also when was greater() added, id think my book would have known and anticipated this.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    So what would u call this? A scoping or namespace error?
    A name clash. These name clashes are the reasons namespaces were introduced.

    Also when was greater() added, id think my book would have known and anticipated this.
    Does your book use the "using namespace std;" clause or did you add that.

    Jim

  6. #6
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by jimblumberg View Post
    A name clash. These name clashes are the reasons namespaces were introduced.
    Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...



    Does your book use the "using namespace std;" clause or did you add that.
    Jim
    It uses the "using namespace std;" convention. The book is C++ By Dissection: Ira Pohl: 9780201787337: Amazon.com: Books Not a big fan of it though, but I started it and I have mild OCD so I want to finish it and then move on to Prata's C++ Primer Plus and Accelerated C++.

    I'm glad I got this problem though, this was good to know info.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...
    You mean like this?
    Code:
    /usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
    rational.cpp:48:9: error: reference to ‘greater’ is ambiguous
    rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
    rational.cpp:28:15: error:                 double greater(double, double)
    rational.cpp:24:12: error:                 int greater(int, int)
    jimblumberg and stahta01 like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by Salem View Post
    > Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...
    You mean like this?
    Code:
    /usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
    rational.cpp:48:9: error: reference to ‘greater’ is ambiguous
    rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
    rational.cpp:28:15: error:                 double greater(double, double)
    rational.cpp:24:12: error:                 int greater(int, int)
    Hahahha yes, like that. Haven't gotten to templates and such yet so that was yiddish to me. Thanks though.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by hex_dump View Post
    Hahahha yes, like that. Haven't gotten to templates and such yet so that was yiddish to me. Thanks though.
    Even if you don't seem to understand a compiler error, always read it.
    Once I had a nasty template error and naturally, skipped it and just posted it here!
    It turned out that the error message *literally* said what I had to do to get rid of it.
    hex_dump likes this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by hex_dump View Post
    Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...
    But it's not technically a clash. You brought in more symbols with the name greater from the std namespace, which peacefully coexists with what you wrote. But when you call it, the compiler doesn't know which one you meant, hence ambiguous.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    If it's not a name clash then what is it?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    I don't know the exact terminology, I was just responding to why the compiler shouldn't say "omg, your Y clashes with X."
    hex_dump likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by whiteflags View Post
    If it's not a name clash then what is it?
    The correct terminology is "ambiguity" or (as used by the compiler in the original post) "ambiguous". The error message meant that, in the code which called the function, there is more than one possible candidate function to be called, and the compiler has no reason to prefer one over the other.

    The compiler is correct in using that terminology, because that is the terminology used by the C++ standard. The standard is not using that terminology in some unusual manner either - in the English language, "ambiguity" is a noun meaning "doubtfulness or uncertainty of meaning or intention". The word "ambiguous" is an adjective meaning "open to or having several possible meanings or interpretations" or "of doubtful or uncertain nature".



    It is often tiring in forums to see members debating the terminology for something that the standard defines. People seem to delight in introducing new terminology that - they claim - is clearer than the standard but, in reality, simply obfuscates what is going on.
    Last edited by grumpy; 03-30-2013 at 10:59 PM.
    Salem, ZuK, stahta01 and 1 others like this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compilation error on function overloading..
    By sanddune008 in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2010, 11:09 AM
  2. operator overloading error
    By manzoor in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2008, 06:43 AM
  3. Error when overloading...
    By webren in forum C++ Programming
    Replies: 15
    Last Post: 10-16-2005, 04:15 PM
  4. Overloading = gives me an error
    By KonArtis in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2004, 10:07 AM
  5. A little on Function Overloading
    By zbap in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2002, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21