【发布时间】:2015-06-17 08:35:24
【问题描述】:
#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;
}
我试图用双一替换函数的浮点版本,然后它开始工作。这里有什么问题? 3.14不能算float吗?
错误:重载 'square(double)' 的调用不明确
注意:候选人是:
注意: int square(int)
注意:float square(float)
【问题讨论】:
-
转换为
(float)或在您的文字 (3.14f) 中使用f如果您愿意。我建议使用double,因为它具有更高的精度,并且它是here 中提到的默认浮点数。
标签: c++ overloading