c++关于函数重载的一个问题,好心人讲下这个程序
来源:百度知道 编辑:UC知道 时间:2024/06/10 19:32:30
#include<iostream>
using namespace std;
main()
{
fun(int)
{
cout<<"fun int";
}
fun(double)
{
cout<<"fun double";
}
fun(float)
{
cout<<"fun float";
}
fun(2);
}
这个函数的目的我是想看看c++是怎么分辨int,double,float这三个类型的,比如说我给函数一个2作为实参,它把2当作int还是double处理啊?晕的是这个程序居然还通不过,请高手在解释程序错误的同时也解释一下2是int型还是double型的啊?
using namespace std;
main()
{
fun(int)
{
cout<<"fun int";
}
fun(double)
{
cout<<"fun double";
}
fun(float)
{
cout<<"fun float";
}
fun(2);
}
这个函数的目的我是想看看c++是怎么分辨int,double,float这三个类型的,比如说我给函数一个2作为实参,它把2当作int还是double处理啊?晕的是这个程序居然还通不过,请高手在解释程序错误的同时也解释一下2是int型还是double型的啊?
敢问你在main内部定义函数 能对么!!
#include<iostream>
using namespace std;
void fun(int i)
{
cout<<"fun int";
}
void fun(double d)
{
cout<<"fun double";
}
void fun(float f)
{
cout<<"fun float";
}
main()
{
fun(2);
getchar();
getchar();
}
1、#include<iostream.h>
2、main()要么返回空要么返回一 你没有返回
3.fun(t)这里的t是参数,必须是具体的值 你int float double 不能直接做参数
4.2是int 不是double 但2.0是double/float 不是int
LZ,你学c++多久了,居然在函数里定义函数!!