2014-12-27 104 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