对重载函数'pow'的模糊调用 [英] Ambiguous call to overloaded function 'pow'
问题描述
运行以下代码时遇到了一些问题。我得到了这个:错误C2668:'pow':对重载函数的模糊调用。我尝试使用static_cast手动将参数转换为合适的类型,但是我认为我得到了一些指针错误?!
程序应该将基数16
#define _CRT_SECURE_NO_WARNINGS
#include< stdio.h>
#include< stdlib.h>
#include< conio.h>
#include< string.h>
#include< math.h>
//基地16到基地10
int转换(char * n){
int结果= 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);
}
返回结果;
}
void main(void){
char n [10];
printf(Introduceti numarul:); scanf(%s,n);
printf(Numarul in baza 10 este:%d,convert(n));
_getch();
}
这些都是错误。
1> ------建立开始:Project:pr8,配置:Debug Win32 ------
1> pr8.cpp
1>错误C2668:'pow':对超载函数的模糊调用
1>可以是'long double pow(long double,int)throw()'
1>或'long double pow(long double,long double)throw()'
1>或'float pow(float,int)throw()'
1>或'float pow(float,float)throw()'
1>或'double pow(double,int)throw()'
1>或'double pow(double,double)'
1>而试图匹配参数列表'(int,size_t)'
1>' - ':只能从另一个指针中减去指针
1>错误C2668:'pow':对超载函数的模糊调用
1>可以是'long double pow(long double,int)throw()'
1>或'long double pow(long double,long double)throw()'
1>或'float pow(float,int)throw()'
1>或'float pow(float,float)throw()'
1>或'double pow(double,int)throw()'
1>或'double pow(double,double)'
1>而试图匹配参数列表'(int,size_t)'
1>错误C2668:'pow':对超载函数的模糊调用
1>可以是'long double pow(long double,int)throw()'
1>或'long double pow(long double,long double)throw()'
1>或'float pow(float,int)throw()'
1>或'float pow(float,float)throw()'
1>或'double pow(double,int)throw()'
1>或'double pow(double,double)'
1>尝试匹配参数列表'(int,size_t)'
==========构建:0成功,1失败,0最新,0跳过===== =====
我该如何解决这个问题?在 C 语言中,我们可以在math.h下找到库函数。
: double pow(double x,double y)---- 1 **
在 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
由于您使用的是C风格的编程,但是使用C ++编译器进行编译,因此编译器可能会遇到数学库中带有已定义函数的模糊状态,因此您应该根据为函数定义 1 ,因此将您的代码更改为,
result + =(n [ i] - 'a'+ 10)* pow(16.0,static_cast< double>(strlen(n) - i - 1))
还有NOTED
在给定的代码段中存在 Perreal p>
if(n [i]> ='A')
result + =(n [i] - A + 10)* pow(16,strlen(n)-i-1);
你不能对字符串进行算术运算。 / em>
,还将
int result
更改为 double result
if您需要高精度和准确的结果。
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.
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<double>(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.
这篇关于对重载函数'pow'的模糊调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!