Thread: ambiguity error caused by function overload?

  1. #1
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32

    ambiguity error caused by function overload?

    Hi there,

    I just finished this and had a question.

    I was asked to "create a void function called round() that rounds the value of its double argument to the nearest whole value. Have round() use a reference parameter and return the rounded result in this parameter."

    Here's my code (which works fine now):

    Code:
    // Rounding Program
    
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    void rounD (double &val); // function prototype
    
    int main()
    {
        double input;
        
        cout << "Enter a value: ";
        cin >> input;
        rounD (input);
        cout << "\n\nRounded value is: " << input;
    
        cin.ignore();
        cin.get();
        return 0;
    }
    
    
    /*Functions*/
    
    void rounD (double &val)
    {
        double intPart;
        double *pintPart;
        pintPart = &intPart;
        
        double fracPart = modf(val, pintPart);
        
        /* couts used in debugging - really helped!*/
        cout << "Val: " << val << "\n";
        cout << "fracPart: " << fracPart << "\n";
        cout << "intPart: " << intPart << "\n";
        
        if (fracPart >= 0.5) val = (intPart + 1);
        else val = intPart;
    }
    It works fine now but at first I kept getting an ambiguity error when I called the function. I knew this happens when you overload a function and feed it an ambiguous value. But I only had one function! I figured, then, that there must be another function in <cmath> with the same name... so I just changed the name of my function slightly and it compiled with no problems. After that I searched the <cmath> header but there doesn't seem to be a round() function in there at all!

    So my question is: if my ambiguity error was caused by function overload, where did this other mysterious, invisible, trouble-making function come from? Or, if the error was not caused by function overload, what did cause it?

    Guess that's a bit of a long way to ask a short question. Sorry for that. Any comments would be really appreciated, though.

    Thanks

    Ps. I'm using Dev-C++ on Windows XP

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Yes I think overload cause it. Look at your MSDN and you'll find:

    Returns an integer closest in value to the argument.

    number round(number)
    Remarks
    If there are two such numbers, the one that is closest to positive infinity is returned. If the argument is NaN, NaN is returned. If the argument is positive infinity, positive infinity is returned. If the argument is negative infinity, negative infinity is returned. If the argument is positive zero, positive zero is returned. If the argument is negative zero, negative zero is returned. If the argument is less than zero but greater than or equal to -0.5, negative zero is returned.

    For the last two cases, the result of calling the round() function is not the same as the result of adding 0.5 and then calling the floor() function because positive zero will be returned in such cases.

    The following function call returns 3.

    round(2.6)
    The following function call returns 2.

    round (2.4)
    The following function call returns 3.

    round(2.5)
    The following function call returns –2.

    round(-1.6)
    The following function call returns –1.

    round(-1.5)

  3. #3
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hi Micko,

    Thanks for the reply.

    I read your response over several times but I don't think it answers my question (maybe I'm missing something though?). Can you explain why my version of round () gave an ambiguity error but when I changed its name to rounD () there was no error? To me, this seems to indicate that another function labled round () has been included somewhere... but I just can't figure out where it comes from.

    Since it compiled when I used a different name for the function I don't think the problem lies in negative numbers.

    In what header is the round() that you mention above?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Micko was saying that there was another function called round that was causing the ambigous error, and then he printed out the information from MSDN about the function.

    I actually think that information refers to a round function that has nothing to do with C++, but it is still possible that somewhere in one of the includes possibly nested deep in the implementation of cmath or another header, that a function called round is used and conflicting with your round function.

    This is actually where namespaces help. If the conflicting function is in the namespace std, then if you remove the using namespace std; and use one of the other alternatives you might be able to use your function called round.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > After that I searched the <cmath> header but there doesn't seem to be a round() function in there at all!
    Oh it's in there all right, if you dig deep enough into all the nested include files.

    Handy tip for figuring out what is in include files
    Code:
    #include <cmath>
    Save that single line as a source file then do
    Code:
    g++ -o hello.i -E hello.cpp
    Everything will be expanded and unravelled as the compiler would see it in the hello.i file.
    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.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    No, there is no such function in the <cmath> header but there is such function described in msdn, so I think this issues is Visual studio .net specific!
    Cheers!

  7. #7
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Thanks jlou and Salem.

    It never occurred to me that it could be nested so deeply.

    Salem, I tried your suggestion but I don't think I'm using it right. I couldn't get it to work. I saved the <cmath> file but didn't know what to do with the other bit. I'm using Dev C++ and since I couldn't find any way to use your command there I switched to the command line to input it. But, alas, it didn't work. This sounds like a great little tool but I think I need a little bit more help implementing it.

  8. #8
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Thanks Micko.

    I did search MSDN a while back but could only find XML references to round (). I didn't see any C++. Though I'm sure I probably just missed it. Could you post the link to theC++ version?

  9. #9
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I must adimt I was wrong. You're right Elhaz, it is only XML reference. I'm sorry if I mislead you, it wasn't intentionaly

  10. #10
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    No worries Micko. Not a problem. I do appreciate your comments though. It gave me another angle from which to work on figuring it out.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM