C++ std::vector :无法将 'std::ostream {aka std::basic_ostream<char>}' 左值绑定到 'std::basic_ostream<char>&&'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24609915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
std::vector : cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
提问by tinlyx
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)
(使用带有 -std=c++11 的 gcc-4.8.1 测试)
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.
SO 有类似的问题,如Overloading operator<<: cannot bind lvalue to 'std::basic_ostream<char>&&',这是关于一些用户定义的带有嵌套类的类。还有一个围绕该问题的公认答案的工作。
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?
但我不知道这是否适用于std::vector
. 有人可以解释为什么会发生这个错误std::vector
,以及如何解释它?
Thanks
谢谢
回答by Angew is no longer proud of SO
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).
与模板相关的错误消息有时会令人困惑。问题是标准库没有定义operator <<
for 插入std::vector
(或任何其他容器,就此而言)到std::ostream
. 因此,编译器无法为 找到合适的重载operator <<
,并尽其所能报告此失败(不幸的是,这在您的情况下不太好/不太可读)。
If you want to stream an entire container, you can use std::ostream_iterator
for that:
如果要流式传输整个容器,可以使用std::ostream_iterator
:
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.
显然有一个模板重载,operator<<
它接受一个 lhs 类型std::ostream&&
的参数和一个模板类型的 rhs 参数;它的存在是为了允许插入临时流。由于它是一个模板,它成为您代码中表达式的最佳匹配。然而,std::cout
是一个左值,所以它不能绑定到std::ostream&&
。因此错误。
回答by Ali
This a known issue with gcc, I filed an enhancement request regarding this.
这是 gcc 的一个已知问题,我提交了关于此的增强请求。
The "only" problem is that the thing you are trying to print out to the console has no operator<<
. Unfortunately, the error message is not very helpful. :(
“唯一”的问题是您尝试打印到控制台的内容没有operator<<
. 不幸的是,错误消息不是很有帮助。:(
By the way, the question has nothing to do with vector
or l-value and r-value references. A minimal example:
顺便说一下,这个问题与vector
l 值和 r 值引用无关。一个最小的例子:
#include <iostream>
struct A { };
int main() {
A a;
std::cout << a;
}
See the discussion at the enhancement request for the gory details. In short, the gcc developers had already tried to improve the error message but it proved to be notoriously difficult.
有关血腥细节,请参阅增强请求中的讨论。简而言之,gcc 开发人员已经尝试改进错误消息,但事实证明这是非常困难的。
For what it is worth, clang's error messagewith libc++ is clearer in my opinion:
对于它的价值,在我看来,clang与 libc++的错误消息更清楚:
clang++ -std=c++11 -stdlib=libc++ -lc++abi main.cpp && ./a.out
main.cpp:7:15: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'A')
std::cout << a;
~~~~~~~~~ ^ ~
Here, the first line clearly says what the problem is.
在这里,第一行清楚地说明了问题所在。
回答by Vlad from Moscow
There is no defined operator <<
for class std::vector
in class std::basic_ostream
.
operator <<
classstd::vector
中没有为 class定义std::basic_ostream
。
What you want is the following
你想要的是以下
for ( int x : std::vector<int> { 1, 2, 3 } ) std::cout << x << ' ';
std::cout << std::endl;
Though it can be written simpler
虽然可以写得更简单
for ( int x : { 1, 2, 3 } ) std::cout << x << ' ';
std::cout << std::endl;