Ambiguous call to overloaded function 'pow'

前端 未结 4 1199
逝去的感伤
逝去的感伤 2020-12-20 18:55

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 t

4条回答
  •  不知归路
    2020-12-20 19:49

    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(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.

提交回复
热议问题