int add(int x, int y) {return x+y;} float add(double x, double y) {return x+y;} void main() {float f1=1.2,f2=2.3;cout<<add(f1,f2);}该程序为何不会编译时会出错}
时间: 2024-03-24 20:38:45 浏览: 21
该程序会出现编译错误,因为在调用 `add` 函数时,传递了 `float` 类型的参数,但是该函数只定义了 `int` 和 `double` 类型的参数。编译器无法确定应该调用哪个函数,因此会出现错误。为了解决这个问题,我们可以将第二个 `add` 函数的参数类型改为 `float`,这样就可以匹配到对应的函数了,如下所示:
```
int add(int x, int y) {return x+y;}
float add(float x, float y) {return x+y;}
void main() {float f1=1.2,f2=2.3;cout<<add(f1,f2);}
```
这样就不会出现编译错误了。
相关问题
定义一组重载函数,分别实现一组int、float、double型数据的相加功能
可以定义如下一组重载函数:
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
这样就可以分别实现int、float、double型数据的相加功能。
定义一组重载函数,分别实现一组int,float,double型数据的相加功能
可以定义如下重载函数:
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
这样就可以分别实现int,float,double型数据的相加功能。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)