Ambiguous call to overloaded function 'pow'

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

    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.

提交回复
热议问题