重载函数的调用不明确,double vs float

call of overloaded function is ambiguous, double vs float

每当我 运行 这个代码...

#include <iostream>

int add(int x, int y){
    return x+y;
} 

float add(float x, float y){
    return x+y;
}

int main(){
    using namespace std;
    add(1.11, 1.11);
    return 0;
}

...我收到此错误:

18.cpp: In function ‘int main()’:
18.cpp:24:16: error: call of overloaded ‘add(double, double)’ is ambiguous
  add(1.11, 1.11);
                ^
18.cpp:24:16: note: candidates are:
18.cpp:7:5: note: int add(int, int)
 int add(int x, int y){
     ^
18.cpp:11:7: note: float add(float, float)
 float add(float x, float y){

我认为 1.11 显然是一个浮点数,而不是整数。当我将 float 更改为 double 时,程序运行。

为什么 C++ 说调用不明确?

在 C++ 中,像 1.11 这样的十进制文字类型被定义为双精度。鉴于它必须将 double 转换为 intfloat,这会导致歧义。

带有 f 后缀的文字,如 1.11f 将具有 float 类型。