Use*_*ame 2 c++ double overloading
每当我运行这段代码时...
\n\n#include <iostream>\n\nint add(int x, int y){\n return x+y;\n} \n\nfloat add(float x, float y){\n return x+y;\n}\n\nint main(){\n using namespace std;\n add(1.11, 1.11);\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n...我收到此错误:
\n\n18.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n18.cpp:24:16: error: call of overloaded \xe2\x80\x98add(double, double)\xe2\x80\x99 is ambiguous\n add(1.11, 1.11);\n ^\n18.cpp:24:16: note: candidates are:\n18.cpp:7:5: note: int add(int, int)\n int add(int x, int y){\n ^\n18.cpp:11:7: note: float add(float, float)\n float add(float x, float y){\nRun Code Online (Sandbox Code Playgroud)\n\n我认为 1.11 显然是一个浮点数,而不是整数。当我更改float为时double,程序就可以运行。
为什么C++说调用不明确?
\n在 C++ 中,十进制文字的类型1.11被定义为 double。鉴于它必须将 double 转换为intor ,float这会导致歧义。
f带有后缀的文字1.11f具有 float 类型。