I tried the code from this question C++ std::transform() and toupper() ..why does this fail?
#include <iostream>
#include <algorithm>
int main() {
std::string s="hello";
std::string out;
std::transform(s.begin(), s.end(), std::back_inserter(out), std::toupper);
std::cout << "hello in upper case: " << out << std::endl;
}
Theoretically it should’ve worked as it’s one of the examples in Josuttis’ book, but it doesn’t compile http://ideone.com/aYnfv.
Why did GCC complain:
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> > >,
std::back_insert_iterator<std::basic_string
<char, std::char_traits<char>, std::allocator<char> > >,
<unresolved overloaded function type>)’
Am I missing something here? Is it GCC related problem?
Just use
::toupperinstead ofstd::toupper. That is,toupperdefined in the global namespace, instead of the one defined instdnamespace.Its working : http://ideone.com/XURh7
Reason why your code is not working : there is another overloaded function
toupperin the namespacestdwhich is causing problem when resolving the name, because compiler is unable to decide which overload you’re referring to, when you simply passstd::toupper. That is why the compiler is sayingunresolved overloaded function typein the error message, which indicates the presence of overload(s).So to help the compiler in resolving to the correct overload, you’ve to cast
std::toupperasThat is, the following would work:
Check it out yourself: http://ideone.com/8A6iV