简体   繁体   English

对重载函数'pow'的模糊调用

[英]Ambiguous call to overloaded function 'pow'

I'm having some problems runnning the following code. 我在运行以下代码时遇到了一些问题。 I got this: error C2668: 'pow' : ambiguous call to overloaded function. 我得到了这个:错误C2668:'pow':对重载函数的模糊调用。 I've tried to manually cast the arguments to the appropiate type using static_cast, however I think I get some pointer errors?! 我试图使用static_cast手动将参数转换为适当的类型,但是我想我得到一些指针错误?!

The program should convert a number from base 16 to base 10. 该程序应将数字从基数16转换为基数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. 谢谢。

strlen return type is size_t in C++. strlen返回类型是C ++中的size_t 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 : main应该返回int而不是void

int main(void) { 

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. 虽然您将问题标记为C问题,但实际上您将程序编译为C ++程序,因为它是允许重载函数的C ++。

In your case the C++ compiler is unable to select an appropriate overloaded function pow. 在您的情况下,C ++编译器无法选择适当的重载函数pow。 The error message clear shows what functions the compiler considers. 错误消息clear显示编译器考虑的功能。 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 . 考虑到在C / C ++函数中main应该有返回类型int

In C the function is defined as 在C中,函数定义为

int main( void ) {

while in C++ it is usually defined as 而在C ++中,它通常被定义为

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' 而不是字符串文字“A”应该有字符文字'A'

In C language we can find library function under math.h : C语言中,我们可以在math.h下找到库函数:

double pow(double x, double y) ---- 1**

In C++ language we able to have set of overloaded functions under cmath such as: C ++语言中,我们可以在cmath下设置一组重载函数,例如:

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, 由于您使用C风格编程但使用C ++编译器编译,编译器可能会遇到数学库中具有已定义函数的歧义状态,因此您应该根据上面提到的函数定义1适当地转换您的参数,因此将代码更改为,

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 与你给出的代码片段风陵渡有一个错误,因为Perreal注意

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. 你不能用字符串文字进行算术运算。如Ptefan所提到的那样改变 ,如果你需要高精度和准确的结果,也可以将int result改为double result结果。

C++98 provides the following overloaded versions of pow : C ++ 98提供了以下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. 您正在使用的第一个参数16可以转换为这些函数中使用的第一个参数的任何类型。 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. 如果您能够使用C++11 ,则可以使用现有代码。 It has an overload: 它有一个过载:

double pow (Type1 base      , Type2 exponent);

where Type1 and Type2 are arithmetic types . 其中Type1Type2算术类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM