简体   繁体   中英

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. I tried on linux and it failed giving the error below. 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?

Since C++11 this code fails to compile because there is no matching overload for operator<< with both operands of type std::stringstream .

However, prior to C++11, std::ostream provided implicit conversion to void * , so the following overload could be called:

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.

Probably your friend used an old compiler, or a compiler running in old compatibility mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM