首页 > 解决方案 > Visual C++ 形式的字符串数组

问题描述

我在 Microsoft Visual Studio 中创建了空的 C++/CLI 表单。

我想做的是将汽车品牌保持在一个阵列中,而将车型保持在另一个阵列中。然后我会画一个品牌,把它贴在标签上,一个来自那个品牌的车型和三个来自其他品牌的车型。

在我的表单中,我想创建 2 个数组:

String brands[7]={"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};
String models[7][5]={{"Benz","Vito","AMG","Klasa A","Klasa E"},
                     {"Astra","Corsa","Insignia","Zafira","Mokka"},
                     {"Avensis","Corolla","Yaris","Auris","RAV4"},
                     {"126p","Panda","Punto","500","Tipo"},
                     {"A4","A6","Q7","R8","A7"},
                     {"Megane","Captur","Scenic","Kadjar","Espace"},
                     {"Golf","Passat","Tiguan","Beetle","Touran"}};

我无法将它放在 MyForm.h 文件中的任何位置,我收到 E2022 错误。你会建议我什么简单的解决方案?

#pragma once

namespace Project1 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Podsumowanie informacji o MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: W tym miejscu dodaj kod konstruktora
            //
        }


    protected:
        /// <summary>
        /// Wyczyść wszystkie używane zasoby.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected:
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Button^  button3;
    private: System::Windows::Forms::Button^  button4;
    private: System::Windows::Forms::Button^  button5;
    private: System::Windows::Forms::Label^  label1;
    String marki[7]={"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};
    String modele[7][5]={{"Benz","Vito","AMG","Klasa A","Klasa E"},
                     {"Astra","Corsa","Insignia","Zafira","Mokka"},
                     {"Avensis","Corolla","Yaris","Auris","RAV4"},
                     {"126p","Panda","Punto","500","Tipo"},
                     {"A4","A6","Q7","R8","A7"},
                     {"Megane","Captur","Scenic","Kadjar","Espace"},
                     {"Golf","Passat","Tiguan","Beetle","Touran"}};

    private:
        /// <summary>
        /// Wymagana zmienna projektanta.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Wymagana metoda obsługi projektanta — nie należy modyfikować 
        /// zawartość tej metody z edytorem kodu.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->button4 = (gcnew System::Windows::Forms::Button());
            this->button5 = (gcnew System::Windows::Forms::Button());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->SuspendLayout();
            // 
            // button1
            // 

            this->button1->Location = System::Drawing::Point(12, 108);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(147, 23);
            this->button1->TabIndex = 1;
            this->button1->UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(178, 108);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(146, 23);
            this->button2->TabIndex = 1;
            this->button2->UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this->button3->Location = System::Drawing::Point(12, 183);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(147, 23);
            this->button3->TabIndex = 1;
            this->button3->UseVisualStyleBackColor = true;
            // 
            // button4
            // 
            this->button4->Location = System::Drawing::Point(178, 183);
            this->button4->Name = L"button4";
            this->button4->Size = System::Drawing::Size(146, 23);
            this->button4->TabIndex = 1;
            this->button4->UseVisualStyleBackColor = true;
            // 
            // button5
            // 
            this->button5->Location = System::Drawing::Point(178, 250);
            this->button5->Name = L"button5";
            this->button5->Size = System::Drawing::Size(146, 23);
            this->button5->TabIndex = 1;
            this->button5->Text = L"Nastepne";
            this->button5->UseVisualStyleBackColor = true;
            this->button5->Click += gcnew System::EventHandler(this, &MyForm::button5_Click);
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(140, 28);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(0, 13);
            this->label1->TabIndex = 2;
            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(361, 306);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->button5);
            this->Controls->Add(this->button4);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Name = L"MyForm";
            this->Text = L"QUIZ";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion

private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {

}
};
}

标签: arraysstringc++-cli

解决方案


这是做的方法:

首先在受保护的部分声明您的数组(就像您在那里所做的那样)。

array<String^>^ marki;
array<String^,2>^ modele; //Note the declration 2 next to String^. It is rank of the array. 2 for two dimensional.

并在您的构造函数 ( MyForm()) 中初始化您的数组,如下所示。

//SINGLE DIMENSION ARRAY
marki = gcnew array<String^> {"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};

//MULTI DIMENSIONAL ARRAY
modele = gcnew array<String^,2> {{"Benz","Vito","AMG","Klasa A","Klasa E"},
                     {"Astra","Corsa","Insignia","Zafira","Mokka"},
                     {"Avensis","Corolla","Yaris","Auris","RAV4"},
                     {"126p","Panda","Punto","500","Tipo"},
                     {"A4","A6","Q7","R8","A7"},
                     {"Megane","Captur","Scenic","Kadjar","Espace"},
                     {"Golf","Passat","Tiguan","Beetle","Touran"}};
        }

测试:

String^ test = modele[2,4]; //RAV4

推荐阅读