C ++变量重载歧义

时间:2015-09-15 03:00:10

标签: c++

对于以下代码行:

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

我一直收到错误

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

我还尝试过投放incceil(log10(i))以使其无效。思考?

2 个答案:

答案 0 :(得分:2)

是什么让您认为投射incceil会有所帮助?编译器告诉您它无法确定您是否需要log10(float)log10(double)。你需要向编译器明确说明

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

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

答案 1 :(得分:1)

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

正如@ John3136评论的那样,错误是指log10函数的输入。在这种情况下,i变量是int类型。由于int既不是float也不是double,并且两种转换同样可行,编译器不知道选择哪个。因此,您必须明确选择一个。例如:

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