Why “transform(s.begin(),s.end(),s.begin(),tolower)” can't be complied successfully?
From: http://stackoverflow.com/questions/5539249/why-transforms-begin-s-end-s-begin-tolower-cant-be-complied-successfu
 
 
Why “transform(s.begin(),s.end(),s.begin(),tolower)” can't be complied successfully?
| up vote 7 down vote favorite 3 | #include<iostream>
#include<cctype>
#include<string>
#include<algorithm>
using namespace std;main()
{string s("ABCDEFGHIJKL");transform(s.begin(),s.end(),s.begin(),tolower);cout<<s<<endl;
} the 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>)'` What the "unresolved overloaded function type" means? if I replace the tolower of a function written by myself ,it can work.c++ 
 | ||||
| ? | 
 | 
4 Answers
active oldest votes| up vote 8 down vote accepted | try using ::tolower. This fixed the problem for me 
 | ||||||||
| ? | 
 | 
| up vote 13 down vote | The problem most probably relates with multiple overloads of tolower and the compiler is unable to select one for you. You can try qualifying it to select an specific version of it, or you might need to provide a function pointer cast to disambiguate. The tolower function can be present (multiple different overloads) in the <locale> header, as well as in <cctype>. Try:int (*tl)(int) = tolower; // Select that particular overload transform(s.begin(),s.end(),s.begin(),tl ); That can be done in a single line with a cast, but it is probably harder to read:transform(s.begin(),s.end(),s.begin(),(int (*)(int))tolower ); 
 | ||||||||||||||||||||
| ? | 
 | 
| up vote 2 down vote | Browsing my <ctype> header from gcc 4.2.1, I see this:// -*- C++ -*- forwarding header.// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 // Free Software Foundation, Inc. ...#ifndef _GLIBCXX_CCTYPE #define _GLIBCXX_CCTYPE 1#pragma GCC system_header#include <bits/c++config.h> #include <ctype.h>// Get rid of those macros defined in <ctype.h> in lieu of real functions. #undef isalnum #undef isalpha ...#undef tolower #undef toupper_GLIBCXX_BEGIN_NAMESPACE(std)using ::isalnum;using ::isalpha; ...using ::tolower;using ::toupper;_GLIBCXX_END_NAMESPACE#endif So it looks like tolower exists in both the std (from <cctype>) and root (from <ctype.h>) namespaces. I'm not sure what the #pragma does. 
 | ||||
| ? | 
 | 
| up vote 2 down vote | David already identified the issue, namely a conflict between: 
 Using the first is much easier, but is undefined behavior (unfortunately) as soon as you deal with anything else than lower-ascii (0-127) in signed chars. By the way, I do recommend defining char as unsigned. The template version would be nice, but you would have to use bind to provide the second parameter, and it's bound to be ugly... So, may I introduce the Boost String Algorithm library ? And more importantly: boost::to_lower :)boost::to_lower(s); Expressiveness is desirable. 
 | ||||||||||||||||||||
| ? | 
 | 
總結(jié)
以上是生活随笔為你收集整理的Why “transform(s.begin(),s.end(),s.begin(),tolower)” can't be complied successfully?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 数据库安装【MySQL 2059错误和o
- 下一篇: IDEA配置JDK1.8
