Ambiguous call to overloaded function 'pow'

前端 未结 4 1190
逝去的感伤
逝去的感伤 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:36

    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'

提交回复
热议问题