2014-12-07 64 views
-1

所有其他帖子都告訴我要更改我的編譯器,但由於我應該使用此編譯器,所以我無法更改。請幫忙!如何將ostringstream對象複製到另一個(使用gcc-4.7.0)

void foo(ostringstream &os) { 
    ostringstream temp; 
    temp << 0; 
    //do something 
    os.swap(temp); 
} 

^我可以」真正上傳DO-事物的一部分,因爲它是一個學校項目的一部分,但是編譯器是給我一個錯誤的位置:os.swap(臨時);

我也試過=運營商,沒有工作,以及

+2

很難提供一個解決方案,沒有看到你想在你的代碼來完成什麼。請**編輯**您的帖子並在其中包含_relevant_部分代碼。 – 2014-12-07 04:01:49

+0

對不起... – soochism 2014-12-07 04:08:28

+0

您發佈的示例中的簡單解決方案是直接使用'os'而不是第二個'temp'流,後者必須複製到'os'。 – sth 2014-12-07 14:41:04

回答

1

可以使用str成員函數中std::ostringstream既從臨時流設置緩衝區傳遞給foo得到緩衝。

#include <iostream> 
#include <string> 
#include <sstream> 

void foo(std::ostringstream &os) 
{ 
    std::ostringstream temp; 

    temp << "goodbye"; 

    //do something 

    os.str(temp.str()); // Set the new buffer contents 
} 


int main() 
{ 
    std::ostringstream out; 

    out << "hello"; 
    std::cout << out.str() << std::endl; 
    foo(out); 
    std::cout << out.str() << std::endl; 
} 

或者,你可以,只要你進入foo並直接輸出到os清零os緩衝消除使用臨時流。您的帖子沒有提供足夠的信息來確定這對您是否有用,但它是一個選項。

void foo(std::ostringstream &os) 
{ 
    os.str(""); // Set the new buffer contents 

    os << "goodbye"; 

    // do something 
} 
相關問題