【C++错误处理】no matching function for call to transform

简介: 作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 初学C++哈,不知道这个错误是不是很silly,高手轻拍。情况如下: #include #include #include using namespace std; int ma...

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

初学C++哈,不知道这个错误是不是很silly,高手轻拍。情况如下:

#include 
#include 
#include 

using namespace std;

int main (int argc, char * const argv[]){
  string str = "Hello";
  transform(str.begin(), str.end(), str.begin(), toupper);
  cout << str << endl;
  
  return 0;
}

程序的意思很简单,去把Hello都转换为大写。

编译死活不通过:

$ g++ -g -Wall strToUpper.cpp -o strToUpper
strToUpper.cpp: In function ‘int main(int, char* const*)’:
strToUpper.cpp:9: error: no matching function for call to ‘transform(__gnu_cxx::__normal_iterator, std::allocator > >, __gnu_cxx::__normal_iterator, std::allocator > >, __gnu_cxx::__normal_iterator, std::allocator > >, )’</CHAR*,></CHAR*,></CHAR*,>

后来查明原因如下——

我们先看看这个函数的定义:

template   OutIter transform(InIter start, InIter end, OutIter result, Func unaryFunc)

它要求参数和返回值都要是char。Linux中将toupper实现为一个宏而不是函数:
/usr/lib/syslinux/com32/include/ctype.h:

/* Note: this is decimal, not hex, to avoid accidental promotion to unsigned */
#define _toupper(__c) ((__c) & ~32)
#define _tolower(__c) ((__c) | 32)

__ctype_inline int toupper(int __c)
{
return islower(__c) ? _toupper(__c) : __c;
}

__ctype_inline int tolower(int __c)
{
return isupper(__c) ? _tolower(__c) : __c;
}

有三种解决方法:

1.因为在全局命名空间中有实现的函数(而不是宏),所以我们明确命名空间,这并不是总奏效,但是在我的g++环境中没有问题:

transform(str.begin(), str.end(), str.begin(), ::toupper);

2.自己写一个函数出来—wraper

inline char charToUpper(char c)
{
    return std::toupper(c);
}

3.强制转化:将toupper转换为一个返回值为int,参数只有一个int的函数指针。

transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
5月前
|
存储 编译器 C语言
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题(下)
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题
85 5
|
28天前
|
存储 算法 程序员
C++ 11新特性之function
C++ 11新特性之function
24 9
|
5月前
|
安全 C++
C++中的异常处理与错误处理机制
C++中的异常处理与错误处理机制
62 0
|
2月前
|
文字识别 Linux Swift
多图理解,更懂中文,支持function call的Phi-3.5来了!
微软继今年4月推出Phi-3系列小型语言模型后,又一鼓作气三连发布并开源其「小而美」系列 Phi-3.5模型!
|
3月前
|
存储 C++ 运维
开发与运维函数问题之使用C++标准库中的std::function来简化回调函数的使用如何解决
开发与运维函数问题之使用C++标准库中的std::function来简化回调函数的使用如何解决
44 6
|
3月前
|
存储 C++
【C++】string类的使用③(非成员函数重载Non-member function overloads)
这篇文章探讨了C++中`std::string`的`replace`和`swap`函数以及非成员函数重载。`replace`提供了多种方式替换字符串中的部分内容,包括使用字符串、子串、字符、字符数组和填充字符。`swap`函数用于交换两个`string`对象的内容,成员函数版本效率更高。非成员函数重载包括`operator+`实现字符串连接,关系运算符(如`==`, `&lt;`等)用于比较字符串,以及`swap`非成员函数。此外,还介绍了`getline`函数,用于按指定分隔符从输入流中读取字符串。文章强调了非成员函数在特定情况下的作用,并给出了多个示例代码。
|
5月前
|
程序员 编译器 C++
C++中的函数重载(Function Overloading)
C++中的函数重载(Function Overloading)
59 2
|
5月前
|
算法 编译器 C语言
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题(中)
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题
56 2
|
5月前
|
算法 编译器 C语言
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题(上)
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题
37 1
|
4月前
|
存储 Java 程序员
【C++航海王:追寻罗杰的编程之路】异常——错误处理方式之一
【C++航海王:追寻罗杰的编程之路】异常——错误处理方式之一
26 0

热门文章

最新文章