在 visual studio c++ 中定义通用字符串

defining a general string in visual studio c++

我用 C++ VS2010 编写了一个项目。它有一些 GUI,然后我想将一些变量(字符串)从一个函数传递给另一个函数。准确的说,我定义了一个两个checkbox为

    this->M1_GR1->AutoSize = true;
    this->M1_GR1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0)));
    this->M1_GR1->Location = System::Drawing::Point(7, 27);
    this->M1_GR1->Margin = System::Windows::Forms::Padding(2, 4, 2, 4);
    this->M1_GR1->Name = L"M1_GR1";
    this->M1_GR1->Size = System::Drawing::Size(73, 19);
    this->M1_GR1->TabIndex = 95;
    this->M1_GR1->Text = L"M1-GR1";
    this->M1_GR1->UseVisualStyleBackColor = true;
    this->M1_GR1->CheckedChanged += gcnew System::EventHandler(this,&config::M1_GR1_CheckedChanged);

    this->M1_GR2->AutoSize = true;
    this->M1_GR2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9,System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,             static_cast<System::Byte>(0)));
    this->M1_GR2->Location = System::Drawing::Point(7, 63);
    this->M1_GR2->Margin = System::Windows::Forms::Padding(2, 4, 2, 4);
    this->M1_GR2->Name = L"M1_GR2";
    this->M1_GR2->Size = System::Drawing::Size(73, 19);
    this->M1_GR2->TabIndex = 96;
    this->M1_GR2->Text = L"M1-GR2";
    this->M1_GR2->UseVisualStyleBackColor = true;
    this->M1_GR2->CheckedChanged += gcnew System::EventHandler(this,&config::M1_GR2_CheckedChanged);

然后这些复选框有一个条件。如果用户 select 其中一个将变灰,然后另一个字符串 M1_GR1->TextM1_GR1->Text 将被保存。

private: System::Void M1_GR1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
            String^ M1_GR;
            if (M1_GR1->Checked == true) {
                M1_GR2->Enabled = false;
                M1_GR = M1_GR1->Text;
            }
            if (M1_GR1->Checked == false) {
                M1_GR2->Enabled = true;
            }
        }

private: System::Void M1_GR2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
            String ^ M1_GR;
            if (M1_GR2->Checked == true) {
                M1_GR1->Enabled = false;
                M1_GR = "M1-GR2";
            }
            if (M1_GR2->Checked == false) {
                M1_GR1->Enabled = true;
            }
        }

现在如果用户选中了一个后面的框,我想将一个字符串保存在 *.csv 文件中,比如 M1-GR1:

private: System::Void config_Load(System::Object^  sender, System::EventArgs^  e) {
             load_csv();
         }
         void load_csv()
         {
             StreamReader^ sr = File::OpenText("config.csv");
             String^ buff = "";
             array<Char>^chars = {','};
             array<String^>^ arr;

             while (buff = sr->ReadLine()) {
                 arr = buff->Split(chars);
                 if (buff!=""){
                     M1_GR = arr[0];
                 }
                 else
                 {

                 }
             }
             sr->Close();
         }

当我想调试代码时出现以下错误:

config.h(1800): error C2065: 'M1_GR' : undeclared identifier

你能告诉我如何将字符串 M1_GR 传递给 M1_GR 吗?

非常感谢您的提前评论!

附言。在下面 link 中上传了示例版本。通过:测试

http://www.mediafire.com/download/3j5302m17ngvrvn/test.zip

从表面上看,您似乎收到了该错误,因为您试图突然使用 M1_GR,而没有声明它(如错误消息所述)。 如果我正确理解你的问题并且你正在尝试保存 M1_GR 字符串,你在选中一个复选框时保存在 .csv 文件中,那么我会改变一些事情:

  1. 将 M1_GR(通过引用)作为参数传递给 M1_GR1_CheckedChanged() 和 M1_GR2_CheckedChanged() 像这样

private: System::Void M1_GR1_CheckedChanged(System::Object^ sender, System::EventArgs^ e, String% M1_GR) {<br> 如果(M1_GR1->选中==真){ M1_GR2->启用=假; M1_GR = M1_GR1->正文; } 如果(M1_GR1->选中==假){ M1_GR2->启用=真; } }</p> <p>private: System::Void M1_GR2_CheckedChanged(System::Object^ sender, System::EventArgs^ e, String% M1_GR) { 如果(M1_GR2->选中==真){ M1_GR1->启用=假; M1_GR = "M1-GR2"; } 如果(M1_GR2->选中==假){ M1_GR1->启用=真; } }

你最初在两个函数中声明它的方式使它只是函数的局部,因此在你的函数失去它们的范围后它并不真正存在。

  1. 至于没有。 1 上面,将字符串引用传递给你的 config_Load() 函数并将相同的参数传递给你的 load_csv() 函数(在这种情况下,我会按值传递它,因为看起来你不是对它是否被修改感兴趣)。

  2. 最后,我会稍微改变 load_csv() 的实现方式,如下所示:

void load_csv(字符串M1_GR) { //StreamReader^ sr = File::OpenText("config.csv"); //-- 使用 File::OpenWrite() 而不是 File::OpenText() //--OpenText() 打开文件只是为了阅读,但我们想写入它 //-- 只探索 Open() 以获得更大的灵活性</p> <pre><code> FileStream^ fs = File::OpenWrite("config.csv"); //-- I didn't understand what the code below is trying to do so I commented it out /*String^ buff = ""; array<Char>^chars = {','}; array<String^>^ arr; while (buff = sr->ReadLine()) { arr = buff->Split(chars); if (buff!=""){ M1_GR = arr[0]; } else { } } sr->Close();*/ try { //Better memory allocation can be achieved by allocating just enough bytes one need array<Byte>^ M1_GR_bytes = (gcnew UTF8Encoding(true)->GetBytes(M1_GR); fs->Write(M1_GR_bytes , 0, M1_GR_bytes->Length); //--Write M1_GR to your file } finally { if (fs) delete (IDisposable^) fs; } }