简体   繁体   English

C ++变量重载歧义

[英]C++ variable overloading ambiguity

For the following line of code: 对于以下代码行:

for (int i = 1; i <= var; i++) { double inc = (14.0) - double(ceil(log10(i)))};

I keep getting the error 我不断收到错误

Overloading ambiguity between "std::log10(double)" and "std::log10(float)"

I've also tried casting both inc and ceil(log10(i)) to float to no avail. 我也尝试过将incceil(log10(i))都强制转换为无济于事。 Thoughts? 有什么想法吗?

What makes you think casting inc or ceil will help? 是什么让您认为铸造incceil会有所帮助? The compiler is telling you that it can't figure out whether you want log10(float) or log10(double) . 编译器告诉您无法确定要使用log10(float)还是log10(double) You need to make that clear to the compiler 您需要向编译器说明

double inc = (14.0) - double(ceil(log10((float)i)));

or 要么

double inc = (14.0) - double(ceil(log10((double)i)));
Overloading ambiguity between "`std::log10(double)`" and "`std::log10(float)`"
                                           ^^^^^^                     ^^^^^

As @John3136 commented the error is referring to the input of the log10 function. 正如@ John3136所评论的那样,该错误是指log10函数的输入。 In this case that is the i variable which is an int type. 在这种情况下,这是i变量,它是一个int类型。 Since, int is neither a float or a double and both conversions are equally viable the compiler doesn't know which to choose. 由于int既不是float也不是double float ,并且两种转换都同样可行,因此编译器不知道选择哪种方式。 Therefore, you have to explicitly select one. 因此,您必须明确选择一个。 For example: 例如:

std::log10(static_cast<float>(i));

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

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