<i id='2knwI'><tr id='2knwI'><dt id='2knwI'><q id='2knwI'><span id='2knwI'><b id='2knwI'><form id='2knwI'><ins id='2knwI'></ins><ul id='2knwI'></ul><sub id='2knwI'></sub></form><legend id='2knwI'></legend><bdo id='2knwI'><pre id='2knwI'><center id='2knwI'></center></pre></bdo></b><th id='2knwI'></th></span></q></dt></tr></i><div id='2knwI'><tfoot id='2knwI'></tfoot><dl id='2knwI'><fieldset id='2knwI'></fieldset></dl></div>
  1. <small id='2knwI'></small><noframes id='2knwI'>

        <bdo id='2knwI'></bdo><ul id='2knwI'></ul>

      <tfoot id='2knwI'></tfoot>
    1. <legend id='2knwI'><style id='2knwI'><dir id='2knwI'><q id='2knwI'></q></dir></style></legend>
    2. std::transform() 和 toupper(),没有匹配的函数

      时间:2024-05-11
    3. <tfoot id='7lwHD'></tfoot>
          <tbody id='7lwHD'></tbody>

        <small id='7lwHD'></small><noframes id='7lwHD'>

          1. <legend id='7lwHD'><style id='7lwHD'><dir id='7lwHD'><q id='7lwHD'></q></dir></style></legend>

              <i id='7lwHD'><tr id='7lwHD'><dt id='7lwHD'><q id='7lwHD'><span id='7lwHD'><b id='7lwHD'><form id='7lwHD'><ins id='7lwHD'></ins><ul id='7lwHD'></ul><sub id='7lwHD'></sub></form><legend id='7lwHD'></legend><bdo id='7lwHD'><pre id='7lwHD'><center id='7lwHD'></center></pre></bdo></b><th id='7lwHD'></th></span></q></dt></tr></i><div id='7lwHD'><tfoot id='7lwHD'></tfoot><dl id='7lwHD'><fieldset id='7lwHD'></fieldset></dl></div>
                <bdo id='7lwHD'></bdo><ul id='7lwHD'></ul>

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

                上一篇:我应该为 FIFO 使用哪个 STL 容器? 下一篇:使用 std::fill 填充带有递增数字的向量

                相关文章

                <tfoot id='b4bJ1'></tfoot>

                  <legend id='b4bJ1'><style id='b4bJ1'><dir id='b4bJ1'><q id='b4bJ1'></q></dir></style></legend>
                    <bdo id='b4bJ1'></bdo><ul id='b4bJ1'></ul>

                  <small id='b4bJ1'></small><noframes id='b4bJ1'>

                  1. <i id='b4bJ1'><tr id='b4bJ1'><dt id='b4bJ1'><q id='b4bJ1'><span id='b4bJ1'><b id='b4bJ1'><form id='b4bJ1'><ins id='b4bJ1'></ins><ul id='b4bJ1'></ul><sub id='b4bJ1'></sub></form><legend id='b4bJ1'></legend><bdo id='b4bJ1'><pre id='b4bJ1'><center id='b4bJ1'></center></pre></bdo></b><th id='b4bJ1'></th></span></q></dt></tr></i><div id='b4bJ1'><tfoot id='b4bJ1'></tfoot><dl id='b4bJ1'><fieldset id='b4bJ1'></fieldset></dl></div>