[The overload parameters of c ++ functions are int and float, but the 3.14 error is returned.], float3.14
# Include <iostream. h>
Class Base
{
Public:
Void f (int x) {cout <"Base: f (int)" <x <endl ;}
Void f (float x) {cout <"Base: f (float)" <x <endl ;}
};
Void main (void)
{
Base * pb = new Base ();
Pb-> f (3.14 );
// Vs2013 compilation Error
// G: \ program \ c ++. cpp (82): error C2668: "Base: f": the call to the overloaded function is not clear.
// 1> g: \ program \ c ++ \ Base. h (11): it may be "void Base: f (int )"
// 1> g: \ program \ c ++ \ Base. h (10): Or "void Base: f (float )"
// 1> when you try to match the Parameter List "(double )"
}
?? Why? The reason is that the input parameter 3. 14. in vs2013, 3.14 is a constant of the double type by default. If double can be implicitly converted to float or int, the compiler reports an error. Which function can be called? Modify it to pb-> f (3.14f), and then explicitly declare it as the float type. There is no call ambiguity.