<legend id='FGqWH'><style id='FGqWH'><dir id='FGqWH'><q id='FGqWH'></q></dir></style></legend>

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

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

    2. <tfoot id='FGqWH'></tfoot>

          <bdo id='FGqWH'></bdo><ul id='FGqWH'></ul>
      1. std::vector :无法将“std::ostream {aka std::basic_ostream<cha

        时间:2024-08-14
        <i id='FNzCY'><tr id='FNzCY'><dt id='FNzCY'><q id='FNzCY'><span id='FNzCY'><b id='FNzCY'><form id='FNzCY'><ins id='FNzCY'></ins><ul id='FNzCY'></ul><sub id='FNzCY'></sub></form><legend id='FNzCY'></legend><bdo id='FNzCY'><pre id='FNzCY'><center id='FNzCY'></center></pre></bdo></b><th id='FNzCY'></th></span></q></dt></tr></i><div id='FNzCY'><tfoot id='FNzCY'></tfoot><dl id='FNzCY'><fieldset id='FNzCY'></fieldset></dl></div>
        <tfoot id='FNzCY'></tfoot>

                <bdo id='FNzCY'></bdo><ul id='FNzCY'></ul>
                  <tbody id='FNzCY'></tbody>
              • <small id='FNzCY'></small><noframes id='FNzCY'>

              • <legend id='FNzCY'><style id='FNzCY'><dir id='FNzCY'><q id='FNzCY'></q></dir></style></legend>
                  本文介绍了std::vector :无法将“std::ostream {aka std::basic_ostream<char>}"左值绑定到“std::basic_ostream<char>&amp;&"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在尝试做一些像

                  std::cout << std::vector<int>{1,2,3};
                  

                  说的是

                   cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
                   int main() {  std::cout << std::vector<int>{1,2,3}; }
                  

                  (使用 gcc-4.8.1 和 -std=c++11 测试)

                  (tested using gcc-4.8.1 with -std=c++11)

                  SO 有类似的问题,例如 重载运算符<<:无法绑定左值到'std::basic_ostream<char>&&',它是关于一些用户定义的带有嵌套类的类.还有一个解决该问题的公认答案的工作.

                  SO has similar questions like Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’, which is about some user defined class with nested classes. There is also a work around the accepted answer to that question.

                  但我不知道这是否适用于 std::vector.有人可以解释为什么 std::vector 会发生这个错误,以及如何解释它?

                  But I don't know whether this applies to std::vector. Can someone explain why this error happens to std::vector, and how to interpret it?

                  谢谢

                  推荐答案

                  与模板相关的错误消息有时会令人困惑.问题是标准库没有定义 operator << 的重载以将 std::vector(或任何其他容器,就此而言)插入std::ostream.因此,编译器无法为 operator << 找到合适的重载,并尽其所能报告此失败(不幸的是,这在您的情况下不太好/不太可读).

                  Template-related error messages can be confusing at times. The problem is that the standard library does not define an overload of operator << for inserting std::vector (or any other container, for that matter) into a std::ostream. So the compiler fails to find a suitable overload for operator <<, and reports this failure as best as it's able (which is unfortunately not too good/readable in your case).

                  如果要流式传输整个容器,可以使用 std::ostream_iterator 为此:

                  If you want to stream an entire container, you can use std::ostream_iterator for that:

                  auto v = std::vector<int>{1, 2, 3};
                  std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
                  

                  <小时>

                  至于为什么你会得到这个神秘的错误,它有助于分析完整的错误消息:


                  As for why you're getting precisely this cryptic error, it helps to analyse the full error message:

                  prog.cpp: In function ‘int main()’:
                  prog.cpp:13:37: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
                    std::cout << std::vector<int>{1,2,3};
                                                       ^
                  In file included from /usr/include/c++/4.8/iostream:39:0,
                                   from prog.cpp:3:
                  /usr/include/c++/4.8/ostream:602:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::vector<int>]’
                       operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
                       ^
                  

                  显然有一个 operator<< 的模板重载,它采用 std::ostream&& 类型的 lhs 参数和模板化类型的 rhs 参数;它的存在是为了允许插入临时流.由于它是一个模板,它成为您代码中表达式的最佳匹配.然而,std::cout 是一个左值,所以它不能绑定到 std::ostream&&.因此出现错误.

                  There is apparently a template overload of operator<< which takes a lhs argument of type std::ostream&& and a rhs argument of the templated type; it exists to allow insertion into temporary streams. Since it's a template, it becomes the best match for the expression in your code. However, std::cout is an lvalue, so it cannot bind to std::ostream&&. Hence the error.

                  这篇关于std::vector :无法将“std::ostream {aka std::basic_ostream<char>}"左值绑定到“std::basic_ostream<char>&amp;&"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:有没有比 std::remove_if 更好的替代方法来从向量中删除元素? 下一篇:将 emplace 与 std::fill 等算法一起使用

                  相关文章

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

                      • <bdo id='vxfzE'></bdo><ul id='vxfzE'></ul>

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