gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:09 25 4
gpt4 key购买 nike

我在运行以下代码时遇到了一些问题。我得到这个:错误 C2668:'pow':对重载函数的调用不明确。我尝试使用 static_cast 手动将参数转换为适当的类型,但我认为我遇到了一些指针错误?!

程序应该将一个数字从 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();
}

这些都是错误。

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 ==========

我该如何解决这个问题?谢谢。

最佳答案

strlen 返回类型是 C++ 中的 size_t。所以你可以通过转换来解决歧义:

pow(static_cast<size_t>(16), strlen(n) - i - 1);

也在这里:

result += (n[i] - "A" + 10)

^ this should be 'A'

并且 main 应该返回 int 而不是 void:

int main(void) { 

关于c++ - 对重载函数的模糊调用 'pow',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24749711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com