This article starts with a source code in C ++ standard library. Today, I saw Chapter 1 "string", so I typed the source code in the book and compiled it in Ubuntu, so I got an error.
What is STL error ...... Information exception! So I went to Google to search for the keyword with an error message. Well, I found the problem, which is also caused by the code in the C ++ standard library.
Now let's take a look at the Code:
# Include <cctype> <br/> # include <iostream> <br/> # include <string> <br/> # include <algorithm> <br/> using namespace STD; <br/> int main (void) <br/>{< br/> string username1 = "wuzhenghua"; <br/> transform (username1.begin (), username1.end (), username1.begin (), tolower); <br/> cout <111 <username1 <222 <Endl; <br/> return 0; <br/>}
The error message is as follows:
Tolower. cpp: In function 'int main ()':
Tolower. CPP: 11: Error: no matching function for call to 'transform (_ gnu_cxx ::__ normal_iterator <char *, STD: basic_string <char, STD :: char_traits <char>, STD: Allocator <char>, _ gnu_cxx ::__ normal_iterator <char *, STD: basic_string <char, STD :: char_traits <char>, STD: Allocator <char>, _ gnu_cxx ::__ normal_iterator <char *, STD: basic_string <char, STD :: char_traits <char>, STD: Allocator <char >>,< unresolved overloaded function type> )'
It is really "complicated" and looks dizzy. Let's take a look at the explanation:
A c-version function tolower, int tolower (INT) is declared in it, and a function template is also declared in the middle:
Template
Chart tolower (Chart C, const locale & LOC );
If both header files are included in the program at the same time (the C ++ standard header file may contain another standard header file. For example, some compilers include or header files in some standard header files. In this way, the include may be introduced or). Because these tolower functions are located in the same STD namespace, a function overload is formed. In this case, when the fourth parameter of the transform function (also a template function) is tolower, the given tolower is only used as a function pointer, there is a lack of information about function parameters required for Type derivation. Therefore, you cannot export the function type and decide which overload function to use.
If you want to use a non-template tolower function, there are multiple solutions:
Transform (S. Begin (), S. End (), S. Begin (), (INT (*) (INT) tolower );
Or
INT (* PF) (INT) = tolower; // PF is a function pointer and its type is clear.
Transform (S. Begin (), S. End (), S. Begin (), Pf );
Or
// Use a packaging function to avoid the overload caused by the direct use of the tolower function.
Int my_tolower (int c)
{
Return tolower (c); // you can determine which overload function of tolower is used based on the C type.
}
//...
// My_tolower is a non-template non-overloaded function, which avoids the type parsing problem caused by function overloading.
Transform (S. Begin (), S. End (), S. Begin (), my_tolower );
In addition, the tolower of non-template functions is actually from the Standard C library function, so it is located in both the global and STD namespace in the C ++ standard library. Since the tolower function in the STD namespace may form a function overload, but there is only one tolower function in the global namespace, you can also directly use the tolower in the global namespace:
Transform (S. Begin (), S. End (), S. Begin (),: tolower );
Of course, the difference between a template function and a non-template function tolower is obvious: the former has two parameters, and the latter has only one parameter. The fourth parameter of the transform function used in the program requires that, if it is a function, only one parameter function can be used, in this regard, the selection of overload functions can be solved. Some compilers may make further discrimination based on this point, or directly select non-template functions to solve this problem. However, the C ++ standard does not require similar Uncertain Problems to be Solved. Therefore, no matter how the compiler handles the issue, the solution, or the solution is not solved, it should be in line with the standard.