簡體   English   中英

錯誤:無法將 'std::basic_ostream' 左值綁定到 'std::basic_ostream&&' sl << ss;

[英]error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ sl << ss;

我的朋友給我發了一個代碼,他說在 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提供了void *的隱式轉換,因此可以調用以下重載:

basic_ostream& operator<<( const void* value );

如果 stream 有錯誤,則 output 將與輸出 null 指針相同,否則一些未指定的非空指針值。

可能您的朋友使用了舊的編譯器,或者以舊的兼容模式運行的編譯器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM