2014-12-27 105 views
0

我在過去的幾個小時內一直在搜索,並且仍然不確定上述問題的答案。代碼如下:用另一種形式寫出一種形式定義的變量的值

optionsForm.h

public: String^ hostScreenOption; 
private: System::Void saveButton_Click(System::Object^ sender, System::EventArgs^ e) { 

      if (hostScreenTrueRadio -> Checked == true) 
      { 
       hostScreenOption = "True"; 
      } 
      else if (hostScreenFalseRadio -> Checked == true) 
      { 
       hostScreenOption = "False"; 
      } 

      Form::Close(); 
    } 

finalForm.h

#include "optionsForm.h" 

String^ name; 
String^ city; 

private: System::Void continueButton_Click(System::Object^ sender, System::EventArgs^ e) { 
     StreamWriter^ optionsWriter = gcnew StreamWriter("Game  Files\\Millionaire\\preferences.txt"); 

      if (nameBox -> Text == "") 
      { 
      warningLabel -> Text = "Please must enter your name!"; 
      } 
      else 
      { 
       name = nameBox -> Text; 
      } 

      if (cityBox -> Text == "") 
      { 
       warningLabel -> Text = "You must enter your city and country!"; 
      } 
      else 
      { 
       city = cityBox -> Text; 
      } 

      optionsWriter -> WriteLine (hostScreenOption); 
      optionsWriter -> WriteLine (name); 
      optionsWriter -> WriteLine (city); 

      delete optionsWriter; 
      Application::Exit(); 
      Process::Start("Game Files\\Millionaire Host Screen.exe"); 
     } 

我有什麼是optionsForm,這與它單選按鈕和標籤中選擇選項的形式(每單選按鈕是真或假)如果單擊true按鈕,則將值「True」作爲字符串分配給hostScreenOption,反之亦然。在finalForm上,用戶輸入他們的名字,城市和國家,然後按下繼續。名稱文本框內的文本和城市文本框內的文本分別分配給字符串變量名稱和城市。最後,所有這些變量都被寫入一個由單獨程序加載的.txt文件。名稱和城市值寫入到.txt文件中,但沒有問題,但hostScreenOption不是。我收到的錯誤是「hostScreenOption - 未聲明的標識符」,我很困惑,因爲我已經聲明它是一個公共變量,幷包含optionsForm.h。你們中的任何一個人能否指出我可能做錯的正確方向?或者什麼可能是我嘗試做的更有效的方式?

回答

0

optionsForm.h中的變量必須是extern,因此:public: extern String^hostScreenOption;。 然後你必須重新定義它finalForm.hString^hostScreenOption;

我不知道這是去工作,因爲我不知道是否hostScreenOption是全球性的定義在一個類中,你必須嘗試。

但是你爲什麼要用String ^?它花費了大量的內存,所以反而嘗試使用booloptionsForm.h extern和全球聲明它,然後執行與上述相同的finalForm.h:用if條件bool hostScreenOption;

至少更換optionsWriter -> WriteLine (hostScreenOption);

if (hostScreenOption) 
    optionsWriter -> WriteLine ("True"); 
else 
    optionsWriter -> WriteLine ("False"); 
+0

感謝您的幫助。 :) – SGCSam 2014-12-28 19:51:08