c++ 重载函数

来源:百度知道 编辑:UC知道 时间:2024/06/29 03:06:51
#include<iostream>
using namespace std;
void see(int a)
{
cout<<"this int";
}
void see(float a)
{
cout<<"this float";
}
void main()
{
int see(3);
float see(54.1);
}
//----请问下为什么出错?

将see(54.1); 改成see(54.1f); 就可以了,将54.1明确为float类型

void main()
{
see(3);
see(54.1);
}

但是会存在二义性,不知道调用哪个

可以

void main()
{
see(3);
see((float)54.1);
}

调用函数时不能指定返回类型。
void main()
{
see(3);
see(54.1f);
}

楼上的说的没错,只不过还有一个小的细节问题:
void see (int a)
{
cout << "this int" << a << endl;
}
这样才能输出带入的参数,不然你要它干嘛

函数定义时都是void,
而调用的时候为什么前面要加int , float;