函数重载中的int和float [英] int and float in function overloading
本文介绍了函数重载中的int和float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个重载函数,如下所示:
I have two overloaded function like below:
void print(int i) { ... }
void print(float f) { ... }
这给我 print(1.2);
:
error: call of overloaded 'print(double)' is ambiguous
谁能解释我为什么?
推荐答案
1.2是双精度字面量而不是浮点数.
1.2 is a double literal not a float.
因此,编译器需要明确的歧义.
So the compiler requires an explicit disambiguation.
1.2f可以正常工作,因为这是一个浮点文字.
1.2f would work as that is a float literal.
这篇关于函数重载中的int和float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文