[The overload parameters of c ++ functions are int and float, but the 3.14 error is returned.], float3.14

Source: Internet
Author: User

[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.

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.