Question

So I realize that #include is necessary, and that there is a pow(x,y) where x^y works...but when I attempted to use pow(2,(num-1)), it kicked back an error...

errorC2668: 'pow' : ambiguous call to overloaded function

the line of code I have for it is as follows

perfect = (pow(2,(num-1))) * (pow(2,num)-1);

Any recommendations?

Thanks in advance

EDIT:

num is indeed declared as an int.

num does have a value, starts at 1 and goes to UINT_MAX

Added an asterisk to equation

Was it helpful?

Solution

The compiler doesn't know which pow() function to call. The overloads listed here gives the following list:

      float pow (       float base,       float exponent );
     double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
      float pow (       float base,         int exponent );
     double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );

The compiler won't guess which one to use. Make it explicit with a casts.

perfect = (pow(2.,(double)(num-1))) < (pow(2.,(double)num)-1);

There may be some extra casts there, but they won't hurt anything.

OTHER TIPS

These are the allowed pow() functions in C++. The problem is that your code has an int as the first argument and C++ doesn't know whether to promote it to a double or long double.

     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 );

Try putting in (double)2 instead of just 2.

perfect = (pow((double)2,(num-1))) * (pow((double)2,num)-1)

Add missing multiplication (or what you want) and also there should be semicolon at the end of the line:

perfect = (pow(2,(num-1))) * (pow(2,num)-1) ;

From error C2668: 'pow' : ambiguous call to overloaded function in VC++ 2005 only, alex.m wrote,

"pow" (any overload) takes a floating point type number (single, double or long double precision) as first argument, not an integer. This is where the error comes from, since the compiler can't guess the way you want your long integer to be converted.

Just try writing a cast expression, like this:

Code Block

c = pow((double)numberOfScansCompleted, 2);

So, if you try pow((double)2,(num-1)), it should work.


Amusing side note, as I typed the beginning of it into Google, "pow ambiguous call to overloaded function" came up as the top suggested search.

Remember, you can also write integer powers of 2 with a shift operator:

int perfect = (1<<(num-1)) * ((1<<num) - 1);

Or with ldexp (also included from <cmath>):

double perfect = ldexp(1.0, num-1) * (ldexp(1.0, num) - 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top