Reputation: 113
I'm having some problems runnning the following code. I got this: error C2668: 'pow' : ambiguous call to overloaded function. I've tried to manually cast the arguments to the appropiate type using static_cast, however I think I get some pointer errors?!
The program should convert a 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();
}
Those are all the 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? Thank you.
Upvotes: 7
Views: 20514
Reputation: 993
In C language we can find library function under math.h:
double pow(double x, double y) ---- 1**
In C++ language we able to have set of overloaded functions under cmath such as:
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 were using C style programming but compiled using C++ compiler,compiler might face with ambiguity states with defined function in math library,therefore you should convert your argument appropriately according to function definition 1 as mentioned above,therefore change your code as,
result += (n[i] - 'a' + 10)* pow(16.0, static_cast<double>(strlen(n) - i - 1))
ALSO NOTED with in your given code snippet there is a mistake as Perreal noted
if (n[i] >= 'A')
result += (n[i] - "A" + 10)* pow(16, strlen(n) - i - 1);
you cannot do arithmetic operations with string literals.change it as Ptefan mentioned
,also change
int result
to double result
if you need high precision and accurate results.
Upvotes: 2
Reputation: 206557
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 are using, 16
, can be converted to any of the types of the first arguments used in those functions. Hence, the compiler cannot resolve the ambiguity. You can resolve the ambiguity by being explicit with the first argument, by 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 are able to use C++11
, you can use your existing code. It has an overload:
double pow (Type1 base , Type2 exponent);
where Type1
and Type2
are arithmetic types.
Upvotes: 0
Reputation: 310910
Though you marked your question as a C question you actually compile your program as a C++ program because it is C++ that allows to overload functions.
In your case the C++ compiler is unable to select an appropriate overloaded function pow. The error message clear shows what functions the compiler considers. To remove the ambiguity you could call the function for example the following way
result += (n[i] - 'a' + 10)* pow( 16.0, strlen(n) - i - 1.0 );
In this case the compiler would use function
double pow(double,double)
Take into account that in C/C++ function main shall have return type int
.
In C the function is defined as
int main( void ) {
while 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 shall be character literal 'A'
Upvotes: 3
Reputation: 97918
strlen
return type is size_t
in C++. So you can resolve the ambiguity via 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) {
Upvotes: 2