问题描述
我想从这个问题 C ++性病的code: :?变换()和toupper()..why这是否失败
#include <iostream>
#include <algorithm>
int main() {
std::string s="hello";
std::string out;
std::transform(s.begin(), s.end(), std::back_inserter(out), std::toupper);
std::cout << "hello in upper case: " << out << std::endl;
}
从理论上讲它应该已经工作,因为它是在约祖蒂斯书中的例子之一,但它并没有编译 http://ideone.com/ aYnfv 。
为什么GCC抱怨:
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> > >,
std::back_insert_iterator<std::basic_string
<char, std::char_traits<char>, std::allocator<char> > >,
<unresolved overloaded function type>)’
我失去了一些东西呢?难道是海湾合作委员会相关的问题?
Am I missing something here? Is it GCC related problem?
推荐答案
只需使用 :: TOUPPER
而不是的std :: TOUPPER
。也就是说,而不是 STD
命名空间中定义了一个 TOUPPER
在全局命名空间中定义。
Just use ::toupper
instead of std::toupper
. That is, toupper
defined in the global namespace, instead of the one defined in std
namespace.
std::transform(s.begin(), s.end(), std::back_inserter(out), ::toupper);
它的工作: http://ideone.com/XURh7
原因,为什么你的code不工作:有一个在命名空间中的另一个重载函数 TOUPPER
STD
这是解析名称时引起的问题,因为编译器无法决定哪些超载你指的是,当你简单地传递的std :: TOUPPER
。这就是为什么编译器说未解决的重载函数类型
错误消息,表示过载(S)。
Reason why your code is not working : there is another overloaded function toupper
in the namespace std
which is causing problem when resolving the name, because compiler is unable to decide which overload you're referring to, when you simply pass std::toupper
. That is why the compiler is saying unresolved overloaded function type
in the error message, which indicates the presence of overload(s).
因此,为了帮助编译器在解析到正确的过载,你投的std :: TOUPPER
为
So to help the compiler in resolving to the correct overload, you've to cast std::toupper
as
(int (*)(int))std::toupper
也就是说,下面的工作:
That is, the following would work:
//see the last argument, how it is casted to appropriate type
std::transform(s.begin(), s.end(), std::back_inserter(out),(int (*)(int))std::toupper);
检查出来自己: http://ideone.com/8A6iV
这篇关于的std ::变换()和toupper(),没有匹配的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!