C++中浮点数据后面一点要加个f吗?
小弟测试了重载函数:
#include <iostream>
using namespace std;
int test(int a,int b);
float test(float a,float b);
void main()
{
cout << test(1,2) << endl << test(2.1f,3.14f) << endl;
cin.get();
}
int test(int a,int b)
{
return a+b;
}
float test(float a,float b)
{
return a+b;
}
如果将cout << test(1,2) << endl << test(2.1f,3.14f) << endl;
改成为cout << test(1,2) << endl << test(2.1,3.14) << endl;
报错:重载函数不能够正确的识别。
请问使用浮点(float数据)后面要加个f吗?
[解决办法]
#include <iostream> using namespace std; int test(int a,int b); float test(float a,float b); void main() { cout<<test(1,2); cout<<test(float(2.1),float(3.14)) << endl; cin.get(); } int test(int a,int b) { return a+b; } float test(float a,float b) { return a+b; }
[解决办法]
如果将cout < < test(1,2) < < endl < < test(2.1f,3.14f) < < endl;
改成为cout < < test(1,2) < < endl < < test(2.1,3.14) < < endl;
报错:重载函数不能够正确的识别。
请问使用浮点(float数据)后面要加个f吗?
=================================================
这个主要是函数重载解析的问题
1.选择候选函数
2.选择可行函数
3.选择最佳可行函数
test(2.1f, 3.14f),对于两个test都是候选的,也都是可行的,但是2.1f,3.14f严格匹配float参数,因为他俩就是float,所以不会有二义性
如果改成test(2.1,3.14),他俩就是double的在选择第三步时,由于double可以向int转换,也可以向float转换,而且转换具有同等地位,所以就会产生二义性