#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 Compile Error
G:\program\c++\c++\c++.cpp (*): Error C2668: "Derived::f": ambiguous call to overloaded function
1> G:\program\c++\c++\derived.h (11): May be "void derived::f (int)"
1> G:\program\c++\c++\derived.h (10): or "void derived::f (float)"
When 1> tries to match the argument list "(double)"
}
?? Why is it? The reason is that the parameter passed in 3.14. In vs2013 3.14 is a constant of type double, then from a double can be implicitly converted to float or int, the compiler will error, there are two functions can be called, in the end with Which? Modified to Pb->f (3.14f), just fine, so explicitly declared as a float type. There is no ambiguity in the call.
"C + + function overload parameters are int and float, but incoming 3.14 error"