Strange ambiguous call to overloaded function error(对重载函数错误的奇怪模棱两可的调用)
问题描述
我正在努力
void function(int y,int w)
{
printf("int function");
}
void function(float y,float w)
{
printf("float function");
}
int main()
{
function(1.2,2.2);
return 0;
}
我收到一个错误错误,例如..
I get an error error like..
error C2668: 'function' : ambiguous call to overloaded function
当我尝试调用 function(1.2,2)
或 function(1,2.2)
时,它打印为int function"
and when I try to call function(1.2,2)
or function(1,2.2)
it is printing as "int function"
请说明function(float y,float w)
什么时候被调用?
Please clarify when will the function(float y,float w)
be called?
推荐答案
看gcc报错信息:
a.cpp:16: error: call of overloaded ‘function(double, double)’ is ambiguous
a.cpp:3: note: candidates are: void function(int, int)
a.cpp:9: note: void function(float, float)
对任一函数的调用都需要截断,这就是为什么两者都不优于另一个的原因.我怀疑你真的想要 void function(double y,double w)
.请记住,在 C/C++ 中,文字和参数传递的默认浮点类型是 double,而不是 float.
A call to either function would require truncation, which is why neither is preferred over the other. I suspect you really want void function(double y,double w)
. Remember that in C/C++, the default floating-point type for literals and parameter passing is double, NOT float.
更新
如果您真的不想将函数签名从float更改为double,您可以随时使用literals 类型为 float.如果您将后缀 f 添加到 浮点数,它们将被输入浮点数.
If you really don't want to change the function signature from float to double, you can always use literals that are typed as float. If you add the suffix f to the floating point numbers, they will be typed float.
您的示例将是 function(1.2f, 2f)
和 function(1, 2.2f)
.
Your examples would then be function(1.2f, 2f)
and function(1, 2.2f)
.
这篇关于对重载函数错误的奇怪模棱两可的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对重载函数错误的奇怪模棱两可的调用


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01