Function Overloading Short & Sweet Query

This is a discussion on Function Overloading Short & Sweet Query within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int max(int number1,int number2); int max(float number1,float number2,); int main() { float decimal; int ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    Function Overloading Short & Sweet Query

    Code:
    #include <iostream>
    using namespace std;
    
    int max(int number1,int number2);
    int max(float number1,float number2,);
    
    int main()
    {
    
     
       float decimal;
       int maxVal= max(5,12);
        decimal= max(11.12,6.8); //COMPILE TIME ERROR
    
    }
    
    int max(int number1,int number2)
    {
    	 int highest;
    	if(number1>number2)
    		   highest=number1;
    	   else
    		   highest=number2;
    	   return highest;
    }
    
    float max(float number11,float number22)
    {
    	float highest;
    
    	if(number11>number22)
    
    		   highest=number11;
    	   else
    		   highest=number22;
    
    	   return highest;
    }
    Compiler gives error saying " ambiguous function overloading" I do not think its ambiguous because int and float data types are totally different. Please exaplain Its compiler bug or what

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    Exact code is as below...
    Code:
    #include <iostream>
    using namespace std;
    
    int max(int number1,int number2);
    float max(float number1,float number2);
    
    int main()
    {
    
     
       float decimal;
       int maxVal= max(5,12);
        decimal= max(11.12,6.8); //COMPILE TIME ERROR
    
    }
    
    int max(int number1,int number2)
    {
    	 float highest;
    	if(number1>number2)
    		   highest=number1;
    	   else
    		   highest=number2;
    	   return highest;
    }
    
    float max(float number11,float number22)
    {
    	float highest;
    
    	if(number11>number22)
    
    		   highest=number11;
    	   else
    		   highest=number22;
    
    	   return highest;
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Quote Originally Posted by forumuser View Post
    Code:
    #include <iostream>
    using namespace std;
    
    int max(int number1,int number2);
    int max(float number1,float number2,);  //<-----  Syntax error
    
    int main()
    {
    
     
       float decimal;
       int maxVal= max(5,12);
        decimal= max(11.12,6.8); //COMPILE TIME ERROR
    
    }
    
    int max(int number1,int number2)
    {
    	 int highest;
    	if(number1>number2)
    		   highest=number1;
    	   else
    		   highest=number2;
    	   return highest;
    }
    
    float max(float number11,float number22)
    {
    	float highest;
    
    	if(number11>number22)
    
    		   highest=number11;
    	   else
    		   highest=number22;
    
    	   return highest;
    }
    Compiler gives error saying " ambiguous function overloading" I do not think its ambiguous because int and float data types are totally different. Please exaplain Its compiler bug or what

    There ya go, that should fix it.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       decimal= max(11.12,6.8); //COMPILE TIME ERROR
    Those values are "double" type. So the compiler won't know what function to use - it can convert double to float or int, but it there's no function that takes double as such - so it doesn't know which of the two present versions to use. If you add an "f" to the end of the number, it will be a float instead, which will allow your compiler to choose the float function. E.g. 11.12f.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    Why 11.12 is not a float but considered as a double?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    11.12f would be a float constant.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As Salem says, 11.12f is a float value, 11.12 is a double. It would have been possible for the compiler designers to make it the other way around - then you would have asked why it doesn't work when you defined a function like double max(double a, double b); passed the undecorated arguments 11.12, 8.6 - because you didn't have 11.12d, 8.6d or whatever the syntax would be.

    There is no way for the compiler to tell the difference between 11.12 as a double or as a float - so you have to define that it is one or the other - the choice was to make the slightly more common double the default. Go beat up some old programmer who came up with the idea if you like, but aside from the potential satisfaction it may give you and the risk of a prison sentence, I don't think it will change anything.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    345 is by default int and not a long data type... For 11.12 the float is the closest match as compared to double So I am confused

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by forumuser View Post
    345 is by default int and not a long data type... For 11.12 the float is the closest match as compared to double So I am confused
    Fine - you just have to accept that float needs a postfix to say that it's float, and integers don't need to be said that they are "normal" integers, but an L if you want to make it long. It's just one example of all the inconsistencies in programming languages - it's been that way for many years, and it will probably continue for many years to come.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    345 is by default int and not a long data type
    Yes, but 345L is a long int literal. According to the C++ Standard:
    "The type of an integer literal depends on its form, value, and suffix. If it is decimal and has no suffix, it has the first of these types in which its value can be represented: int, long int; if the value cannot be represented as a long int, the behavior is undefined. If it is octal or hexadecimal and has no suffix, it has the first of these types in which its value can be represented: int, unsigned int, long int, unsigned long int. If it is suffixed by u or U, its type is the first of these types in which its value can be represented: unsigned int, unsigned long int. If it is suffixed by l or L, its type is the first of these types in which its value can be represented: long int, unsigned long int. If it is suffixed by ul, lu, uL, Lu, Ul, lU, UL, or LU, its type is unsigned long int."

    For 11.12 the float is the closest match as compared to double So I am confused
    According to the C++ Standard:
    "The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed."
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    the declaration and definition are different. Thats what I thought the problem was
    Code:
    ...
    int max(float number1,float number2,);
    ...
    float max(float number11,float number22)
    ...
    edit: also that comma shouldn't be there
    Last edited by avatarofhope2; 10-17-2007 at 07:40 AM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by avatarofhope2 View Post
    the declaration and definition are different. Thats what I thought the problem was
    Code:
    ...
    int max(float number1,float number2,);
    ...
    float max(float number11,float number22)
    ...
    edit: also that comma shouldn't be there
    Good catch. Except the "actual code" that was posted in the second post doesn't have either the extra comma or the inconsistant declaration.

    The problem is quite surely the "double being thought to be float" confusion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Actually the "actual code" still has different declarations and definitions:

    Code:
    ...
    float max(float number1,float number2);
    ... 
    float max(float number11,float number22)
    ...

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mikeman118 View Post
    Actually the "actual code" still has different declarations and definitions:

    Code:
    ...
    float max(float number1,float number2);
    ... 
    float max(float number11,float number22)
    ...
    That is perfectly valid - what you call your parameters in the prototype and what you call them in the function definition (implementation) is completely separate names - in fact, you can - should you wish, not give them any name at all
    Code:
    float max(float,float);
    is fine.

    It's also fine to completely change the names when you declare the prototype and when you define the function:
    Code:
    float max(float a, float b);
    ...
    float max(float x, float y)
    {
       return (x > y)?x : y;
    }
    /// Or if you REALLY want to confuse people:
    float max(float b, float a)
    {
       return (a > b)?a : b;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Mikeman, this doesn't matter at all. For all purposes you could even leave out the variable name completely in declarations:
    Code:
    float max(float, float); //fine
    //...
    float max(float a, float b)
    { //... 
    }
    Seeing that the OP has decided to "decorate" the variable name differently (11 vs 1) in different functions makes me wonder if he might have some kind of misunderstanding too...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  2. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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