c++ - 函数重载中的 int 和 float

标签 c++ floating-point int overloading

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

void print(int i) { ... }
void print(float f) { ... }

print(1.2); 给我这个错误:

error: call of overloaded 'print(double)' is ambiguous 

谁能解释一下为什么?

最佳答案

1.2 是双字面量而不是 float 。

因此编译器需要明确的消歧。

1.2f 可以工作,因为它是一个 float 。

关于c++ - 函数重载中的 int 和 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34208397/

相关文章:

c++ - 为什么 llvm::Module 存储在 unique_ptr 中?

c++ - to_string 不是 std 的成员,g++ (mingw)

c++ - 许多 linux 套接字后服务器卡住

c++ - Google Kickstart 2020问题记录破坏者错误答案

c++ - 如何在C++中安全地将 double 型转换为整数?

.net - 数据集中的可空 Int 列

c++ - Int to Float to Int转换精度损失

python - 在python中删除指数格式的前导0

floating-point - x87 中的扩展(80 位)双浮点,而不是 SSE2 - 我们不会错过它?

mysql - MySQL INT 类型可以是非零 NULL