Qt5.3 compilation error--call of overloaded ' max (int int) ' is ambiguous

Source: Internet
Author: User

Error Description:

Today in the use of Qt to write a C + + function template test program, compile time, compile the time when the following error occurred:

The error description is: In the main function, when the function max () overloaded, there appears (ambiguous) vague, ambiguous, resulting in ambiguous use;

Because the first time you encounter this kind of error, write a piece of content to commemorate it.

The test code is as follows:
#include <iostream>using namespacestd;template<typename t>//TypeName = = Classt Max (T a,t b) {returnA>b?a:b;}intMain () {intA=1, b=2; cout<<max (b) <<Endl; cout<<max (1,2) <<Endl; cout<<max (2,3) <<Endl; cout<<max (1.1f,2.2f) <<Endl; cout<<max (1.11l,2.22l) <<Endl; cout<<max ('A','C') <<Endl; //all of the above are implicitly called Int,float.double.char types, and the following must show the invocation,//because 1 is of type int and 2.0 is a double type, the compiler is not recognized if the data type is not explicitly specified. cout<<max<int> (1,2.0) <<Endl;}

Single from the code, I see nothing wrong, and I remember when I learned C + + template, it is also written, but the original use of the IDE is VC6.0, is not the environment. So I put the code on the VC, test it, the perfect successful run. It seems that the IDE is a different problem. But for QT, there should be a solution, the Internet to find a bit, the original is because of the problem of function name conflict, here I first say the solution.

Workaround:
    1. Declare the namespace at invocation, and change Max (A, A, a, b) to: Max (A, A, b), plus::, avoid conflicts
    2. Change the function name and change the custom function Max to Mymax or something else.
Cause of the problem:

The max function conflicts with the Std::max function of the standard library.

It seems that QT has a standard max () function, and I tested it, and there really is this function.

question:

From this error, we know that the function name we write is easy to conflict with the function in the standard library or other files, so how does C + + handle function name conflicts? This will use the namespace.

Namespaces are an C++de feature that solves the problem of having the same variable name in different files (vendors) in writing large programs. For example: Just like I wrote the function Max and the standard Max, in order to be able to call one of the max () functions accurately, we put these two functions separately in the namespace unit. This makes it possible to call the required function exactly in the form of "namespace:: Function name" .

    • :: Max ();//Call to write Max ()
    • Std::max ();//The max of the standard Template library that is called

We often use using namespace std; the STD which is C + + in the standard namespace.

There are four ways to get the program to access std:

    1. Put the using namespace Std; Before the function definition, let all the functions in the file be able to use the namespace Std in the element.?
    2. The using namespace STD is placed in a specific function definition so that the function can use the element in the namespace Std.?
    3. Using std::cout in a particular function; Such a compilation instruction allows the function to use the namespace std specified in the element, such as cout. The advantage of this approach is that it prevents the variable name you define from being duplicated in the element name in Std.
    4. The use of the compile instruction is not used at all, and the prefix std is used when you need to use an element in std::.

  Through the 4th above, the solution to this problem is another, is not used using namespace std; , but cout,cin and other standard library functions can not be used, so the best way is to modify the function name.

Qt5.3 compilation error--call of overloaded ' max (int int) ' is ambiguous

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.