[英]error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ sl << ss;
My friend sent me a code where he says sucessfuly compiled in Windows.我的朋友给我发了一个代码,他说在 Windows 中成功编译。 I tried on linux and it failed giving the error below.
我在 linux 上进行了尝试,但未能给出以下错误。 Below is a minimum verifiable example of the code.
下面是代码的最小可验证示例。
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
std::stringstream ss, sl;
sl << ss;
}
but it gives但它给了
error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’
sl << ss;
Why it works in windows but not in linux, and why this error happens?为什么它在 windows 中有效,但在 linux 中无效,为什么会发生此错误?
Since C++11 this code fails to compile because there is no matching overload for operator<<
with both operands of type std::stringstream
.自 C++11 以来,此代码无法编译,因为
operator<<
没有匹配的重载与std::stringstream
类型的两个操作数。
However, prior to C++11, std::ostream
provided implicit conversion to void *
, so the following overload could be called:但是,在 C++11 之前,
std::ostream
提供了到void *
的隐式转换,因此可以调用以下重载:
basic_ostream& operator<<( const void* value );
The output would be the same as outputting a null pointer if the stream has an error, otherwise some unspecified non-null pointer value.如果 stream 有错误,则 output 将与输出 null 指针相同,否则一些未指定的非空指针值。
Probably your friend used an old compiler, or a compiler running in old compatibility mode.可能您的朋友使用了旧的编译器,或者以旧的兼容模式运行的编译器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.