简体   繁体   中英

Why does the compiler say pow(n,k) ambiguous in this code?

When compiling the following code under Xcode 4.5 (Mac), compilation fails with the following error message:

call to 'pow' is ambiguous

Why? Thanks!

#include <iostream>
#include <cmath>
#include <limits>

using namespace std;

int main()
{
    cout << "\tSquare root calculator using an emulation the ENIAC's algorithm." << endl
         << endl;

    long long m;
    while ((cout << "Enter a positive integer:" << endl)
            && (!(cin >> m) || m < 0 || m > 9999999999)) //10-digit maxium
    {
        cout << "Out of range.";
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    again:
    long long l = m;

//Find out how big the integer is and adjust k accordingly.
    int order = -1;
    long long temp = m;
    do
    {
        temp /= 10;
        order++;
    } while (temp/10);
    int k = order/2;

//Step 1
    long long a = -1;
    do
    {
        a+=2;
        m -= a*pow(100,k);
    } while (m >= 0);

    while (k > 0)
    {
        k--;

//Step 2
        if (m < 0)
        {
            a = 10*a+9;
            for (;m < 0;a -= 2)
            {
                m += a*pow(100,k);
            }
            a += 2;
        }

//Step 3
        else
        {
            a = 10*a-9;
            for(;m >= 0;a += 2)
            {
                m -= a*pow(100,k);
            }
                 a -= 2;
        }

    }

//Step 4
    cout << endl << "The square root of " << l << " is greater than or equal to "
         << (a-1)/2 << " and less than " << (a+1)/2 << "." << endl << endl;

    while ((cout << "Enter a positive integer to calculate again, or zero to exit." << endl)
            && (!(cin >> m) || m < 0 || m > 9999999999))
    {
        cout << "Out of range.";
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }


    if (m > 0) goto again;

    return 0;
}

This link looks like it answers your question: http://bytes.com/topic/c/answers/727736-ambiguous-call-pow

It looks like the pow() function doesn't like integers.

As you can see here there is no pow(int,int);

pow
    <cmath>
         double pow (      double base,      double exponent );
    long double pow ( long double base, long double exponent );
          float pow (       float base,       float exponent );
         double pow (      double base,         int exponent );
    long double pow ( long double base,         int exponent );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM