【问题标题】:Overloading a function in C++ [duplicate]在C ++中重载函数[重复]
【发布时间】:2016-07-07 18:39:41
【问题描述】:

我正在通过 Sololearn 学习 C++。我对函数重载有疑问

这是代码

#include<iostream>
using namespace std;


void printSomething(int x)   {

   cout << "I'm printing an integer " << x << endl;
}
void printSomething(float x)   {

   cout << "I'm printing a float " << x << endl;
}

int main()  {

   int a =3; 
   float b = 2.65;
   printSomething(a);
   printSomething(b);
   return 0;
}

输出为

               I'm printing an integer 3 
               I'm printing a float 2.65

但是如果我在调用函数时直接给出参数

喜欢这个

#include<iostream>
using namespace std;


void printSomething(int x)   {

   cout << "I'm printing an integer " << x << endl;
}
void printSomething(float x)   {

   cout << "I'm printing a float " << x << endl;
}

int main()  {

   printSomething(3);
   printSomething(2.65);
   return 0;
}

我收到以下错误

..\Playground:在函数“int main()”中: ..\Playground:19:24: 错误: 重载 'printSomething(double)' 的调用不明确 printSomething(2.65); ^ ..\Playground:19:24: 注意:候选人是: ..\Playground:5:6: 注意:无效 printSomething(int) 无效 printSomething(int x) { ^ ..\Playground:9:6: 注意: void printSomething(float) 无效 printSomething(浮动 x){ ^

但如果我改变了

void printSomething(float x)   {

    cout << "I'm printing a float " << x << endl;
}

void printSomething(double x)   {

    cout << "I'm printing a float " << x << endl;
}

我会得到输出

       I'm printing a float 2.65

为什么? 但如果它只是整数,它可以正常工作

#include<iostream>
using namespace std;


void printSomething(int x)   {

   cout << "I'm printing an integer " << x << endl;
}
void printSomething(float x)   {

   cout << "I'm printing a float " << x << endl;
}

int main()  {

    printSomething(3);
    return 0;
}

结果

                      I'm printing an integer 3

为什么这不适用于浮动

谢谢

【问题讨论】:

  • 因为2.65double 文字,而不是float。你可以试试printSomething(2.65f);;
  • @songyuanyo 那么当我将它声明为浮点数时它是如何工作的,如第一个程序所示 int main() { int a =3;浮动 b = 2.65; printSomething(a); printSomething(b);返回0;在这里我声明 2.65 为 float 而不是 double?
  • 因为 bfloat (正如您声明的那样)。
  • 所以如果直接给函数'printSomething(2.65)'会变成双倍吗?
  • 这是因为隐式类型转换。为了避免这种情况,请给出类似 printSomething((float)2.65);

标签: c++


【解决方案1】:

2.65 不是 float 文字,它是 double 文字。

因此编译器不知道您是将double 转换为float 还是int,因此会发出错误。

在您的第一种情况下,当编写 float b = 2.65; 时,编译器假定您知道自己在做什么,并且使用 b 调用重载是明确的。

如果你写了printSomething(2.65f);,那么这也是明确的:2.65ffloat 文字。

【讨论】:

    【解决方案2】:

    2.65 被视为double。但是您没有提供重载void printSomething(double x)。因此编译器必须转换该值,它不知道是否应该转换为floatint(两者都具有精度损失`)。

    如果你写2.65f,它被认为是float,它应该可以工作。

    【讨论】:

      【解决方案3】:

      原因在于转换规则和重载解决策略。如果 C++ 无法找到参数的精确匹配,它会寻找转换。最好的转换是隐式转换,即扩大(将数据类型转换为可以保存原始类型的所有值甚至更多的数据类型),然后是缩小转换(转换为较小的数据类型,这可能会导致错误或某些值的精度损失),然后是用户定义的转换。

      由于文字 2.65 是 double 类型,编译器会查找转换。有两个:double -> float 和 double -> int。它们都在缩小,这意味着它们同样好。编译器无法选择最佳的,因此报告错误。

      要解决此问题,您可以:

      • 按照你的做法定义 double 的重载
      • 使用浮点字面量 (2.65f) 而不是双精度值

      【讨论】:

        猜你喜欢
        • 2011-12-31
        • 2022-12-22
        • 2010-10-27
        • 1970-01-01
        • 2011-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多