Ambiguity error with double as parameter type

This is a discussion on Ambiguity error with double as parameter type within the C++ Programming forums, part of the General Programming Boards category; For the following code I'm getting the following error: line 15 "Ambiguity between 'std::sqrt(double)' and 'sqrt(double)' in function main()" I ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Ambiguity error with double as parameter type

    For the following code I'm getting the following error: line 15

    "Ambiguity between 'std::sqrt(double)' and 'sqrt(double)' in function main()"

    I compared the code with my friend's and they were practically identically. Yet, his got no errors. I copied and pasted his code, compiled it, and ran it. It got the same ambiguity error! The assignment itself is just a square root function and that's not the problem. The code is below:

    Code:
    #include<iostream>
    using namespace std;
    double sqrt(double x){
            double counter = 1;
            double guess = 1;
            while (counter < 20){
                    guess = (guess + (x/guess))/2;
                    counter = counter + 1;
            }
            return guess;
    }
    int main(void){
            double a;
            cin >> a;
            cout << sqrt(a);
            return 0;
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Your friend is probably using a different compiler.

    The problem is that <cmath>, apparently indirectly included through <iostream> on your compiler, defines a function called sqrt already, and due to your using statement, the compiler doesn't know whether you want to call your function or std::sqrt.

    There are two solutions.
    1) Give your own function a different name.
    2) Remove the using statement and either import cin and cout selectively or fully qualify them. But you will probably run afoul of the global sqrt in <math.h> soon. So, it's best if you just don't use names used by the standard library.
    Last edited by CornedBee; 11-11-2007 at 03:19 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The header <cmath> already has a sqrt function. It seems that each compiler treats this kind of thing differently (MingW compiles and calls custom sqrt) and your safest solution might be to rename the function.
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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