0

私は Visual C++ を学んでいますが、1 日目にかなりの障害に遭遇しました。Visual C++ 2010 のフォーム テンプレートを使用して、単純な「Hello World」アプリケーションを作成しようとしています。

このソリューションでは、Form1.h、resource.h、stdafx.h、AssemblyInfo.cpp、helloworld.cpp、および stdafx.cpp が作成されました。ボタンとラベルを Form1 に追加し、ボタンをクリックするとラベルのテキストが Form1.h 内から "Hello World" に変更されるようにすることができました。ただし、これを Form1.cpp から実行したいので、ヘッダーからコードを削除しました。現在、次のコードがあります。

Form1.cpp

#include "stdafx.h"
#include "stdio.h"
#include "Form1.h"

using namespace helloworld;


helloworld::Void testButton_Click(System::Object^  sender, System::EventArgs^  e) {
    helloworld::Form1::label1->Text = "Test";
}

Form1.h

#pragma once

namespace helloworld {

    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>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  testButton;
    public: System::Windows::Forms::Label^  label1;
    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>testButton
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->testButton = (gcnew System::Windows::Forms::Button());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->SuspendLayout();
            // 
            // testButton
            // 
            this->testButton->Location = System::Drawing::Point(136, 159);
            this->testButton->Name = L"testButton";
            this->testButton->Size = System::Drawing::Size(75, 23);
            this->testButton->TabIndex = 0;
            this->testButton->Text = L"Test Button";
            this->testButton->UseVisualStyleBackColor = true;
            this->testButton->Click += gcnew System::EventHandler(this, &Form1::testButton_Click);
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(125, 100);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(35, 13);
            this->label1->TabIndex = 1;
            this->label1->Text = L"label1";
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->testButton);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void testButton_Click(System::Object^  sender, System::EventArgs^  e) ;
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
         }
    };
}

このコードでは、次のエラーが表示されます。

error C2227: left of '->Text' must point to class/struct/union/generic type

私が見つけたものから、そのエラーは、プログラムが私が話していることを知らないことを意味しlabel1->Textます. Visual C++ の初心者に与えられる助けをいただければ幸いです。

4

1 に答える 1

3

問題は次のスニペットにあります。

helloworld::Void testButton_Click(System::Object^  sender, System::EventArgs^  e) {
    helloworld::Form1::label1->Text = "Test";
}

helloworld::Form1フォームのタイプです。実際のインスタンスではありません。キーワードを使用thisして、現在のオブジェクトインスタンスを参照します(ほとんどの場合、thisキーワードはオプションです)。もう1つの問題はhelloworld::Void、である必要がありますSystem::Void。また、これがクラスメソッドであり、フリー関数ではないことを示すためtestButton_Clickに、を置き換える必要があります。Form1::testButton_Clickコードは次のようになります。

System::Void Form1::testButton_Click(System::Object^  sender, System::EventArgs^  e) {
    label1->Text = "Test";
}

注:Windowsフォームを操作する場合は、C#を使用する必要があります。C ++ / CLIは言語の混乱であり、大多数の人々は、マネージドコードとネイティブコードを組み合わせる場合にのみC ++/CLIを使用します。

于 2012-07-22T23:17:46.347 に答える