用于显示不同数字类型的函数重载
function overloading for displaying different number types
我正在尝试制作一个具有重载函数"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);
相关文章:
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- c++:可变模板和函数重载
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- 解决模板成员函数重载
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 推断模板化函数中的函数重载
- C++复制函数重载导致"must be a nonstatic member function"错误
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 可变参数泛型 lambda 和函数重载
- C++中的函数重载和继承
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- C/C++ 可变参数宏函数重载
- 将基类的成员函数重载到其他派生类C++
- C++ 函数重载匹配
- C++函数重载,具体步骤是什么
- C++:使用 param pack 显式调用函数重载
- 隐式生成的函数重载用于右值参数?
- 使用函数重载输入运算符
- 运算符重载函数上的函数重载