函数重载中的 int 和 float

aer*_*ite 3 c++ floating-point int overloading

我有两个重载函数,如下所示:

void print(int i) { ... }
void print(float f) { ... }
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误print(1.2);

error: call of overloaded 'print(double)' is ambiguous 
Run Code Online (Sandbox Code Playgroud)

谁能解释一下为什么?

Bat*_*eba 5

1.2 是双精度文字而不是浮点数。

因此编译器需要显式消歧。

1.2f 可以工作,因为它是浮点文字。