Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
711 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...