c++中重载函数的原型

prototype for overloaded function in c++

本文关键字:原型 函数 重载 c++      更新时间:2023-10-16

为什么我不能像对待其他函数一样,在main()中为重载函数提供原型??例如,我写了以下代码

#include<iostream.h>
#include<conio.h>
void main()
{
 void a(float);
 void a(float,float);
 a(2.4);
 a(5.6,7.4);
getch();
}
void a(float x)
{
 cout<<x;
}
void a(float y,float z)
{
 cout<<y<<z;
} 

在运行此代码时,turbo c++编译器给出了一个错误,我观察到它忽略了函数a的第二个声明,即void a(float,float);,因此在调用a(float)时给了错误额外的参数。

此代码在Wandbox上运行良好。

#include<iostream>
//#include<conio.h>
using std::cout;
int main()
{
 void a(float);
 void a(float,float);
 a(2.4);
 a(5.6,7.4);
//getch();
}
void a(float x)
{
 cout<<x;
}
void a(float y,float z)
{
 cout<<y<<z;
}

你的编译器似乎不那么聪明。请考虑使用另一个编译器。