【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 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
3月前
|
数据采集 自然语言处理 Devops
ToolLearning Eval:CodeFuse发布首个中文Function Call的大语言模型评测基准!🚀
CodeFuse发布了首个面向ToolLearning领域的中文评测基准ToolLearning-Eval,以帮助开发者跟踪ToolLearning领域大模型的进展,并了解各个ToolLearning领域大模型的优势与不足。ToolLearning-Eval按照Function Call流程进行划分,包含工具选择、工具调用、工具执行结果总结这三个过程,方便通用模型可以对各个过程进行评测分析。
289 0
|
5月前
Fatal error: Call to undefined function openssl_pkey_get_private()
Fatal error: Call to undefined function openssl_pkey_get_private()
35 0
|
4月前
|
Python
Python(二十九)pycharm连接调试器失败 Interrupted function call accept failed~
Pycharm在使用调试器模式时报错: Interrupted function call: accept failed
83 0
|
PHP Windows
windows下 Call to undefined function posix_getpid() in ……\Workerman\Worker.php 的解决方法
windows下 Call to undefined function posix_getpid() in ……\Workerman\Worker.php 的解决方法
125 0
windows下 Call to undefined function posix_getpid() in ……\Workerman\Worker.php 的解决方法
|
8月前
|
监控 Linux Apache
访问zabbix安装页面报错500,apache报错Call to undefined function mb_detect_encoding()
访问zabbix安装页面报错500,apache报错Call to undefined function mb_detect_encoding()
142 0
|
9月前
|
PHP
PHP报错Call to undefined function utf8_decode()的解决方案
PHP报错Call to undefined function utf8_decode()的解决方案
|
10月前
|
搜索推荐 Java API
基于OpenAI Function Call 结合Lucene实现本地化的知识搜索
这是一个OpenAI Function 接入的Demo,通过Lucene+OpenAI实现个人知识库,但是目前只有服务端,没有页面之类的东西,并且实现的非常简单,所以只能当做一个Demo使用。
339 0
基于OpenAI Function Call 结合Lucene实现本地化的知识搜索
|
11月前
|
程序员 C++ 开发者
C++异常和错误处理机制:如何使您的程序更加稳定和可靠
在C++编程中,异常处理和错误处理机制是非常重要的。它们可以帮助程序员有效地处理运行时错误和异常情况。本文将介绍C++中的异常处理和错误处理机制。
88 0
|
消息中间件 PHP
laravel6 使用rabbitmq报错:Call to a member function make() on null at Queue\\Jobs\\Job.php:215
laravel6 使用rabbitmq报错:Call to a member function make() on null at Queue\\Jobs\\Job.php:215
132 0

热门文章

最新文章