【问题标题】:Function overloading using int and float type as an argument in c++ [duplicate]在c ++中使用int和float类型作为参数的函数重载[重复]
【发布时间】:2017-07-26 07:58:01
【问题描述】:
void sum(int x);
void sum(float x);

我不明白为什么这些函数重载语句会为 sum(2.3) 创建以下错误:

重载sum(double) 的调用不明确

但是对于下面两个函数重载,效果很好。

void sum(int x);
void sum(double x);

【问题讨论】:

  • 使用sum(2.3f)

标签: c++ floating-point int double overloading


【解决方案1】:

2.3 是双精度数,编译器无法在转换为 int 或 float 之间进行选择(两者都会丢失精度)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多