用于显示不同数字类型的函数重载

function overloading for displaying different number types

本文关键字:函数 重载 类型 数字 显示 用于      更新时间:2023-10-16

我正在尝试制作一个具有重载函数"display"的程序。到目前为止,我还可以,但我似乎被卡住了。我似乎无法弄清楚我在这里的代码做错了什么。

#include <iostream>
#include <iomanip>
using namespace std;
int main()

{
    double small_num, large_num;
    double display;//I think the problem is here!!!
    cout <<"Please enter two number starting with the smallest, then enter the      largest"<<endl;
cin >> small_num;
cin >> large_num;
display(small_num, large_num);

system("pause");
return 0;
}
void display(int num1, int num2)
{
    cout << num1 <<" "<< num2 << endl;
    return;
}
void display(double num1, int num2)
{
    cout << num1 <<" "<< num2 << endl;
}
void display(int num1, double num2)
{
    cout << num1 <<" "<< num2 << endl;
}
void display(double num1, double num2)
{
    cout << num1 <<" "<< num2 << endl;
}

删除double display; .这将创建一个名为 display 的新局部变量,该变量与您的函数名称冲突。

以及以下任一:

  • display方法放在main 以上。
  • 将它们的定义保留在原处,并在 main 上方声明它们,如下所示:

    void display(int num1, int num2);
    void display(double num1, int num2);
    void display(int num1, double num2);
    void display(double num1, double num2);