函数重载做实验时发生的错误,特此记录。
#include<iostream>
namespace test
{
int add(int x,int y)
{
return x+y;
}
float add(float x,float y)
{
return x+y;
}
}
int main()
{
test::add(1,2);
test::add(1.1,2.2);
return 0;
}
问题描述:虽然函数重载了两个add函数,但在编译时会报错。意思就是函数调用不明确。
解决方法:这是因为C++默认把小数的类型识别为double,之后将double转换为int 还是float编译器是不知道的。
将函数重载的参数改为double即可,如下。