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

时间:2013-09-16 02:36:56

标签: c++ function overloading

我正在尝试使用重载函数“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;
}

1 个答案:

答案 0 :(得分:0)

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

还要么:

  • display方法置于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);