#include <iostream>
using namespace std;
int square(int x);
float square(float x);
int main() {
cout<<square(3);
cout<<square(3.14);
return 0;
}
int square(int x) {
cout<<"\nINT version called\n";
return x*x;
}
float square(float x) {
cout<<"\nFLOAT version called\n";
return x*x;
}
Run Code Online (Sandbox Code Playgroud)
我试图用double替换函数的float版本,然后它开始工作.这里有什么问题?不能将3.14视为浮动?
错误:调用重载'square(double)'是模棱两可的
注意事项:候选者是:
注意:int square(int)
注意:float square(float)
C++中的浮点文字属于类型double.转换double为int和float没有定义排序,因此您的调用不明确.
如果要调用该float函数,请使用float文字调用它:
cout<<square(3.14f);
//note the f here^
Run Code Online (Sandbox Code Playgroud)