2012-12-19 56 views
0

一個全球性的ofstream我需要做的某事像這樣:定義在windows窗體

#include <fstream> 

    ... 


ofstream myStream 

    private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) { 
    //open ofstream 
       } 

    private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { 
    //write to ofstream 
    } 

這可能嗎?

在此先感謝。

+0

你有什麼試過讓你覺得你不能這樣做?這對我來說似乎很簡單。 '流'不是全球性的;它只是你的類的一個實例變量。 – prprcupofcoffee

+0

我試圖按照上面的代碼所示聲明ofstream。但這是不允許的。我目前轉向Streamwriter,但我更喜歡使用ofstream。 –

+0

聲明是什麼樣的?錯誤是什麼? – prprcupofcoffee

回答

0

該代碼片段顯示了您想要聲明的位置,但不是聲明的樣子。無論如何,問題在於您不能在託管(.NET CLR)代碼中使用非託管(C++本機)對象。您可能會聲明一個ofstream*變量,但不是ofstreamofstream&

如果你想使用ofstream而不是System.IO.StreamWriter,你必須在一個單獨的包含hative代碼的單獨的類中創建一個類,並在.NET代碼中實例化該類。 Here是一個解決這個問題的CodeProject文章。

+0

感謝David。很高興知道。 –