奇怪的模糊调用重载函数错误 [英] 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)
或函数(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"
请澄清函数)
Please clarify when will the function(float y,float w)
be called?
推荐答案
查看gcc的错误讯息:
Look at the error message from 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函数(double y,double w)
。请记住,在 C / C ++ 中,默认浮点类型为文字和参数传递是双重,不是 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.
UPDATE
如果您真的不想更改 float 到 double 的函数签名,可以始终使用键入为 float 强>。如果您在浮点数字中添加 f 后缀,则会输入float。
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.
然后是函数(1.2f,2f)
和函数(1,2.2f)
。
这篇关于奇怪的模糊调用重载函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!