简体   繁体   中英

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. Thoughts?

What makes you think casting inc or ceil will help? The compiler is telling you that it can't figure out whether you want log10(float) or 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. In this case that is the i variable which is an int type. Since, int is neither a float or a double and both conversions are equally viable the compiler doesn't know which to choose. Therefore, you have to explicitly select one. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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