"C + + function overload parameters are int and float, but incoming 3.14 error"

Source: Internet
Author: User

#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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.