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++
share|improve this question
edited Apr 4 '11 at 13:39
Ken Bloom
27.4k254115

asked Apr 4 '11 at 13:33
liu
312213

 
2  
The return type of main is int, and return types in C++ have to be explicit. Some compilers will allow the code as posted, but it is non-standard, and it might break with the new compiler version or in other compilers. –  David Rodríguez - dribeas Apr 4 '11 at 15:21
add a comment

4 Answers

active oldest votes

up vote 8 down vote accepted

try using ::tolower. This fixed the problem for me

share|improve this answer
answered Apr 4 '11 at 13:48
davka
3,96122152

 
1  
It's right. What's difference between tolower and ::tolower? –  liu Apr 4 '11 at 14:06
 
@liu: it's as @David wrote - the "::" selects the tolower from the global namespace –  davka Apr 4 '11 at 14:16
add a comment
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 );

share|improve this answer
answered Apr 4 '11 at 13:38
David Rodríguez - dribeas
124k5117292

 
3  
But don't forget that using this version of tolower on a string like the above is undefined behavior if any of the char values are negative (which they can be on most modern systems, e.g. if any accented characters are present). –  James Kanze Apr 4 '11 at 13:49
1  
@James Kanze: good point, I decided for that overload from reading the original post (where cctype is explicitly included, while locale is not). Also, the functions in locale take more than a single argument, and that means that the code would add unrelated complexity with a bind or bind2nd to provide the default locale... –  David Rodríguez - dribeas Apr 4 '11 at 13:58
 
Thank you.I understand the problem. And using ::tolower can fix the problem –  liu Apr 4 '11 at 14:11
1  
@liu: Note that using ::tolower will work in different compilers, but it is not standard. Basically most compilers, when you include cctype the compiler is required to provide int std::tolower(int), but it is not required to add int ::tolower(int), different compilers will provide both functions with the same implementation (one of them will forward to the other) but that is not required and might change with the next compiler release (or if you change the compiler) –  David Rodríguez - dribeas Apr 4 '11 at 15:20
1  
@liu Using ::tolower doesn't fix the problem. Calling ::tolower with a char argument is undefined behavior. You need to wrap it in a functional object which converts the char to unsigned char. Or buy into all of the complexity that David Rodríguez mentions with regards to the versions in <locale>. –  James Kanze Apr 4 '11 at 16:40
show 8 more comments
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.

share|improve this answer
answered Apr 4 '11 at 13:49
Mike DeSimone
20.3k23361

 
1  
the pragma signals to gcc that this file is a system header. This should typically affect diagnosis, since it's considered bad style for a compiler to emit warnings for headers that it was bundled with and should not be altered. –  Matthieu M. Apr 4 '11 at 19:12
add a comment
up vote 2 down vote

David already identified the issue, namely a conflict between:

  • <cctype>'s int tolower(int c)
  • <locale>'s template <typename charT> charT tolower(charT c, locale const& loc)

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.

share|improve this answer
answered Apr 4 '11 at 19:22
Matthieu M.
103k9109261

 
 
1) can you please explain what do you mean by I do recommend defining char as unsigned? 2) does boost::to_lower assumes some character set, e.g. latin-1? –  davka Apr 5 '11 at 15:27
 
@davka: 1) the C++ Standard does not precise whether a char is signed or unsigned. You can qualify it if you want to be sure. However a number of functions (like int tolower(int)) have undefined behavior if invoked with a negative char... Look it up on your compiler, there might be a switch or a sane default. 2) boost::to_lower is based on the C++ tolower function, and thus depends on the std::locale and the ctype facet it has been imbued with. Note that these facets cannot handle multi-characters encoding anyway... –  Matthieu M. Apr 5 '11 at 15:37
 
thanks, I still don't get #1. I know about char being implementation-dependent. Are you suggesting typedef unsigned char char? is it legal? –  davka Apr 5 '11 at 15:43
1  
@davka: No, it's not legal. Compilers usually have switches to let you decide, for example gcc has -fsigned-char and -funsigned-char. –  Matthieu M. Apr 5 '11 at 17:00
 
that's interesting, thanks. So, if I use -funsigned-char I should use signed char if I want -128..127, correct? Does it work well with 3rd party libraries that are not compiled with this flag? –  davka Apr 5 '11 at 17:09
show 5 more comments

Why “transform(s.begin(),s.end(),s.begin(),tolower)” can't be complied successfully?相关推荐

  1. SQL 异常处理 Begin try end try begin catch end catch--转

    SQL 异常处理 Begin try end try begin catch end catch 总结了一下错误捕捉方法:try catch ,@@error, raiserror 这是在数据库转换的 ...

  2. MySQL中 begin 事务 begin ,第二个begin带自动提交功能???

    两个begin,第二个begin带自动提交功能吗,不是只能commit和rollback提交结束事务吗?

  3. mysql begin rollback_事务控制语句,begin,rollback,savepoint,隐式提交的SQL语句

    事务控制语句 在MySQL命令行的默认设置下,事务都是自动提交的,即执行SQL语句后就会马上执行COMMIT操作.因此开始一个事务,必须使用BEGIN.START TRANSACTION,或者执行SE ...

  4. 【LaTex】\begin{array}{r},\begin{array}{l},\begin{array}{c}

    r:right y = \left\{ \begin{array}{rr} a & 12345\\ b+x & 78\\ l &910 \end{array} \right. ...

  5. 【C++】STL——string的简单介绍、string类的访问和遍历、operator[] 、begin+ end begin 、rbegin + rend begin 、范围for

    文章目录 1.string类 1.1string类对象的访问及遍历操作 (1)operator[] (2)begin+ end begin (3)rbegin + rend begin (4)范围fo ...

  6. HDU - 5978 To begin or not to begin(简单博弈)

    题目链接:点击查看 题目大意:给出k个黑球以及一个红球,两个人轮流摸球,摸到红球算赢,问先手有优势还是后手有优势还是都一样,若先手优势输出1,若后手优势输出2,若都一样输出0 题目分析:简单博弈,我们 ...

  7. HDU 5978 2016ICPC大连 H: To begin or not to begin

    题意: 黑色盒子里有n个白球,1个红球,两个人轮流取球,先取出红球的胜利 对于不同的n,先手优势输出1,劣势输出2,平等输出0 n=1,显然概率为1/2,平等 n=2,第一发取到白球概率是2/3也就是 ...

  8. 小红帽中用eclipse编译windows程序遇到的问题处理备忘录

    1.size_t无符号整型变量,size_t has not been declared 解决办法加头文件stddef.h 2.expected initializer befor 'a' 当头文件无 ...

  9. linux shell awk BEGIN END 处理文本之前之后执行操作 简介

    目录 简介 1 最简单的action 既没有pattern 有没有options 2 awk是逐行处理 2.1 输出整行 2.2 输出最后一列 2.3 输出倒数第二列 3 pattern 3.1 be ...

最新文章

  1. C++实现动态顺序表
  2. 技术盘点:2022年云原生架构趋势解读
  3. 第二次启用httpd24调用mysql时出现的错误
  4. Seq2Seq中的Attention
  5. keras cnn注意力机制_2019 SSA-CNN(自注意力机制)目标检测算法论文阅读笔记
  6. python条件语句练习题_[python](1)---条件语句练习题
  7. 电子和程序设计学习网址
  8. cad图纸问号怎么转换文字_CAD中文图纸中文字体变成问号怎么办?不慌这几步教你轻松解决...
  9. HttpClient Cookie rejected警告
  10. 瀚高数据库块恢复示例
  11. 如何在众多快递物流中筛选出代收的单号
  12. 虎克哈克环槽铆钉机 铆接回收机振动筛设备 钢结构集装箱铆接机
  13. 第一次实验结论与总结
  14. 森林防火广播系统方案
  15. MCP2517FD应用总结
  16. 单片机原理及应用第三版课后答案张毅刚
  17. 《天空之音VR》即将发售,挥手击中音符就能秒变“节奏大师”
  18. 社会新“毒瘤”:AI骚扰电话
  19. 【白板动画制作软件】万彩手影大师教程 | 添加背景音乐
  20. CentOS-6.3-i386-bin-DVD1.iso下载地址

热门文章

  1. 如何用python做一个贪吃蛇小游戏并给游戏加上背景音乐(pygame的应用)
  2. 惠普打印机无法联网;惠普打印机 HP web 服务打不开;惠普打印机连接Internet失败;eprint无法使用
  3. HTML5期末大作业:web网页设计与开发网站设计——爱奇艺首页(1页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码
  4. Ubuntu 下使用小书匠
  5. 计算机化验证名词解释
  6. wifi 联想小新_WiFi 6网卡?官方辟谣联想小新Air 14 2020只支持WiFi 5
  7. 小程序接入vant Weapp组件的详细步骤
  8. 时序逻辑电路二——数字逻辑实验
  9. 什么是GSP药品经营管理规范?
  10. Home Server