An ambiguous call to the overloaded 'pow' function

I am having trouble running the following code. I got this: error C2668: 'pow': ambiguous call to an overloaded function. I tried to manually apply the arguments to the corresponding type using static_cast, but I think I'm getting some pointer errors ?!

The program should convert the number from base 16 to base 10.

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <math.h> //base 16 to base 10 int convert(char *n){ int result = 0; for (int i = strlen(n) - 1; i >= 0; i--){ if (n[i] >= 'a') result += (n[i] - 'a' + 10)* pow(16, strlen(n) - i - 1); else if (n[i] >= 'A') result += (n[i] - 'A' + 10)* pow(16, strlen(n) - i - 1); else if (n[i] >= '0') result += (n[i] - '0')* pow(16, strlen(n) - i - 1); } return result; } void main(void){ char n[10]; printf("Introduceti numarul: "); scanf("%s", n); printf("Numarul in baza 10 este: %d", convert(n)); _getch(); } 

These are all errors.

 1>------ Build started: Project: pr8, Configuration: Debug Win32 ------ 1> pr8.cpp 1> error C2668: 'pow' : ambiguous call to overloaded function 1> could be 'long double pow(long double,int) throw()' 1> or 'long double pow(long double,long double) throw()' 1> or 'float pow(float,int) throw()' 1> or 'float pow(float,float) throw()' 1> or 'double pow(double,int) throw()' 1> or 'double pow(double,double)' 1> while trying to match the argument list '(int, size_t)' 1>'-' : pointer can only be subtracted from another pointer 1> error C2668: 'pow' : ambiguous call to overloaded function 1> could be 'long double pow(long double,int) throw()' 1> or 'long double pow(long double,long double) throw()' 1> or 'float pow(float,int) throw()' 1> or 'float pow(float,float) throw()' 1> or 'double pow(double,int) throw()' 1> or 'double pow(double,double)' 1> while trying to match the argument list '(int, size_t)' 1> error C2668: 'pow' : ambiguous call to overloaded function 1> could be 'long double pow(long double,int) throw()' 1> or 'long double pow(long double,long double) throw()' 1> or 'float pow(float,int) throw()' 1> or 'float pow(float,float) throw()' 1> or 'double pow(double,int) throw()' 1> or 'double pow(double,double)' 1> while trying to match the argument list '(int, size_t)' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

How can i fix this? Thanks.

+6
source share
4 answers

strlen Return type size_t in C ++. This way you can resolve the ambiguity with casting:

 pow(static_cast<size_t>(16), strlen(n) - i - 1); 

also here:

 result += (n[i] - "A" + 10) ^ this should be 'A' 

and main should return int instead of void :

 int main(void) { 
+2
source

Although you marked your question as a C question, you will actually compile your program as a C ++ program because it is C ++ that allows you to overload functions.

In your case, the C ++ compiler cannot select the corresponding overloaded pow function. The error message shows which functions the compiler considers. To eliminate the ambiguity, you could call a function, for example, as follows.

 result += (n[i] - 'a' + 10)* pow( 16.0, strlen(n) - i - 1.0 ); 

In this case, the compiler will use the function

 double pow(double,double) 

Note that in the C / C ++ function, main must have an int return type.

In C, a function is defined as

 int main( void ) { 

and in C ++ it is usually defined as

 int main() { 

And I think there is a typo

  if (n[i] >= 'A') result += (n[i] - "A" + 10)* pow(16, strlen(n) - i - 1); 

Instead of the string literal "A" there should be a letter "A"

+2
source

In the C language, we can find a library function called math.h:

 double pow(double x, double y) ---- 1** 

In the C ++ language, we can have a set of overloaded functions under cmath, for example:

 float pow( float base, float exp ) ---- 2 double pow( double base, double exp ) ---- 3 long double pow( long double base, long double exp ) ---- 4 float pow( float base, int iexp ) ---- 5 double pow( double base, int iexp ) ---- 6 long double pow( long double base, int iexp ) ---- 7 

Since you used C-style programming, but compiled using the C ++ compiler, the compiler may encounter ambiguity states with a specific function in the math library, so you must accordingly convert your argument according to the definition of function 1 as above, so change your code is like

 result += (n[i] - 'a' + 10)* pow(16.0, static_cast<double>(strlen(n) - i - 1)) 

ALSO NOTED in your given code snippet there is an error noted by Perreal

 if (n[i] >= 'A') result += (n[i] - "A" + 10)* pow(16, strlen(n) - i - 1); 

you cannot perform arithmetic operations with string literals. replace it with Ptefan , also change int result to double result if you need high accuracy and accurate results.

+2
source

C ++ 98 provides the following overloaded versions of pow :

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

The first argument you use, 16 , can be converted to any of the types of the first arguments used in these functions. Therefore, the compiler cannot eliminate the ambiguity. You can resolve ambiguity by specifying its first argument, specifying its type. Any one of the following should work:

 pow(16.0, strlen(n) - i - 1); pow(16.0f, strlen(n) - i - 1); pow(16.0l, strlen(n) - i - 1); 

If you can use C++11 , you can use existing code. It has an overload:

 double pow (Type1 base , Type2 exponent); 

where Type1 and Type2 arithmetic types .

0
source

Source: https://habr.com/ru/post/972218/


All Articles