本文介绍了std::transform() 和 toupper(),没有匹配的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这个问题中的代码 C++ std::transform() 和 toupper() ..为什么会失败?

I tried the code from this question C++ std::transform() and toupper() ..why does this fail?

#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;
}

理论上它应该可以工作,因为它是 Josuttis 书中的示例之一,但它无法编译 http://ideone.com/aYnfv.

Theoretically it should've worked as it's one of the examples in Josuttis' book, but it doesn't compile http://ideone.com/aYnfv.

海湾合作委员会为什么抱怨:

Why did GCC complain:

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>)’

我在这里遗漏了什么吗?是 GCC 相关的问题吗?

Am I missing something here? Is it GCC related problem?

推荐答案

只需使用 ::toupper 而不是 std::toupper.也就是说,toupper 定义在全局命名空间中,而不是在 std 命名空间中定义的.

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

您的代码不工作的原因:在名称空间 std 中有另一个重载函数 toupper 导致解析名称时出现问题,因为编译器无法决定当您简单地传递 std::toupper 时,您指的是哪个重载.这就是为什么编译器在错误消息中说unresolved重载函数类型,这表明存在重载.

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::transform() 和 toupper(),没有匹配的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:15