在 C++ 中执行以下代码以进行函数重载时不会输出
Not output is coming when below code is executed in C++ for function overloading
Below is the code, when I am executing. No output in the console. Not sure why.
I am testing one example of function overloading in C++.
函数重载。创建了 2 个具有不同参数的同名函数。在函数调用期间尝试在 main 中传递整数值时。它运作良好。但是当作为浮点数传递时,既没有错误也没有输出。
#include <iostream>
using namespace std;
int max(int x,int y)
{
cout<<"Entered in max int"<<endl;
if(x>=y)
return x;
else
return y;
}
float max(float x,float y)
{
cout<<"Entered in max float"<<endl;
if(x>=y)
return x;
else
return y;
}
int main()
{
float x;
x=max(5.9,6.7);
}
- 有一些隐式转换(float 到 int),当调用 float type max function.So 你需要指定。
max(5.9f, 6.7f)
首先你没有输出函数的结果,第二你的函数没有被调用,因为你正在使用 using namespace std
所以当你调用 max std::max
时被调用。
要调用您的 max
版本,您必须访问全局范围。像这样 ::max(x, y)
.
Below is the code, when I am executing. No output in the console. Not sure why.
I am testing one example of function overloading in C++.
函数重载。创建了 2 个具有不同参数的同名函数。在函数调用期间尝试在 main 中传递整数值时。它运作良好。但是当作为浮点数传递时,既没有错误也没有输出。
#include <iostream>
using namespace std;
int max(int x,int y)
{
cout<<"Entered in max int"<<endl;
if(x>=y)
return x;
else
return y;
}
float max(float x,float y)
{
cout<<"Entered in max float"<<endl;
if(x>=y)
return x;
else
return y;
}
int main()
{
float x;
x=max(5.9,6.7);
}
- 有一些隐式转换(float 到 int),当调用 float type max function.So 你需要指定。
max(5.9f, 6.7f)
首先你没有输出函数的结果,第二你的函数没有被调用,因为你正在使用 using namespace std
所以当你调用 max std::max
时被调用。
要调用您的 max
版本,您必须访问全局范围。像这样 ::max(x, y)
.