[我的朋友给我发送了一个代码,他说在Windows中成功编译。我在linux上尝试过,但无法给出以下错误。下面是该代码的最小可验证示例。
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
std::stringstream ss, sl;
sl << ss;
}
但它给出了
error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’
sl << ss;
为什么它可以在Windows中工作,但不能在Linux中工作,为什么会发生此错误?
由于C ++ 11,此代码无法编译,因为对于两个类型为operator<<
的操作数,std::stringstream
没有匹配的重载。
但是,在C ++ 11之前,std::ostream
提供了implicit conversion to void *
,因此可以调用以下重载:
void *
如果流有错误,则输出与输出空指针相同,否则输出一些未指定的非空指针值。
[您的朋友可能使用了旧的编译器,或者以旧的兼容模式运行的编译器。