1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > C++中错误no matching function for call to transform

C++中错误no matching function for call to transform

时间:2020-05-24 23:35:55

相关推荐

C++中错误no matching function for call to transform

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

将str转为大写

编译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 > >, )

这个函数的定义:

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.自己重写一个函数

inline char charToUpper(char c)

{

return std::toupper(c);

}

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

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

4.补充。LAMBDA表达式

transform(str.begin(), str.end(), str.begin(), [](int c) {return toupper(c); });

原文:/winting_qiqi/article/details/21397211

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。