|
#include <iostream>
#include <string>
using namespace std;
int max(int a, int b)
{
return a > b ? a : b;
}
float max(float a,float b)
{
return a > b ? a : b;
}
int main()
{
cout << max(5.1,6.1);
}
会报错说 “max”: 对重载函数的调用不明确
1> d:\vs编程的垃圾\hello world\1\1\1.cpp(8): 可能是“float max(float,float)”
1> d:\vs编程的垃圾\hello world\1\1\1.cpp(4): 或 “int max(int,int)”
这是怎么回事啊,这个不是很明确吗,
max(小数)就调用float;
max(整数)就调用int;
cout << max((float)5.1, (float)6.1);
5.1和6.1都是double的
而double 可以自动转换成 int ,或float,
编译器不知道你想调哪一个函数了,直接 强制转换类型,指明你想调的函数
|
上一篇: 关于zlib使用问题,网站用gzip下一篇: VS2017,SetFilePointer函数从后往前读取数据出现错误
|