std :: vector:无法绑定'std :: ostream {aka std :: basic_ostream< char>}'lvalue to'std :: basic_ostream< char>&&' [英] std::vector : cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

查看:1167
本文介绍了std :: vector:无法绑定'std :: ostream {aka std :: basic_ostream< char>}'lvalue to'std :: basic_ostream< char>&&'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试做一些简单的操作时,我遇到了一个令人困惑的错误消息:

  std :: cout<<的std ::矢量< INT> {1,2,3}; 

其中包含

 不能绑定'std :: ostream {aka std :: basic_ostream< char>''lvalue to'std :: basic_ostream< char>&'
int main(){std :: cout<<的std ::矢量< INT> {1,2,3}; }

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



SO有类似的问题,如重载运算符<<:无法将左值绑定到'std :: basic_ostream< char>&&',这与使用嵌套类的一些用户定义类有关。还有一个围绕这个问题接受的答案的工作。



但我不知道这是否适用于 std :: vector

code>。有人可以解释为什么这个错误发生在 std :: vector 以及如何解释它吗?
$ b 谢谢 p>

解决方案

与模板相关的错误消息有时会令人困惑。问题在于标准库没有定义插入 std :: vector 的重载 operator< (或任何其他容器)转换为 std :: ostream 。因此,编译器无法为运算符<< / code>找到合适的重载,并且尽可能地报告这个失败(这在你的情况中不幸是不太好/可读的)。

如果你想流式整个容器,你可以使用 std :: ostream_iterator 表示:

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






至于你为什么要准确地掌握这个神秘错误,它有助于分析完整的错误信息:

  prog.cpp:在函数'int main()'中:
prog.cpp:13:37:error:无法绑定'std :: ostream {aka std :: basic_ostream< char>}'lvalue to'std :: basic_ostream< char>&&'
std :: cout<<的std ::矢量< INT> {1,2,3};
^
从/usr/include/c++/4.8/iostream:39:0包含的文件中,从prog.cpp获得
:3:
/ usr / include / c ++ / 4.8 / ostream:602:5:error:初始化'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>]'
运算符< <(basic_ostream< _CharT,_Traits>&& __os,const _Tp& __x)
^

显然,运算符<< 的模板重载需要lhs参数类型 std :: ostream&& 和模板类型的rhs参数;它允许插入临时流。由于它是一个模板,它将成为代码中表达式的最佳匹配。但是, std :: cout 是一个左值,所以它不能绑定到 std :: ostream&& 。因此,错误。

I encountered a confusing error message when trying to do something as simple as

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

which says

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

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

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.

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?

Thanks

解决方案

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

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

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&lt; char&gt;}'lvalue to'std :: basic_ostream&lt; char&gt;&amp;&amp;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆