0

Form2 の要素を Form1 にアクセス (追加) することは、c++ vs2010 で問題なく機能します。Visual Studio Express 2012 で同じことを最初から試してみると、Form2 は宣言されていない識別子であるというエラーが表示され続けます。何が間違っているのですか?

    #include "stdafx.h"
    #include "Form2.h"
    ....
    
    Form2^ frm = gcnew Form2;
    this->Controls->Add(frm->panel1);

エラー C2065: 'Form2': 宣言されていない識別子

コード形式 1:

「stdafx.h」を含める

「Form2.h」を含める

    #pragma once

    namespace WindowsFormsApplication{

        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^  button1;
        protected: 

        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            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->button1 = (gcnew System::Windows::Forms::Button());
                this->SuspendLayout();
                // 
                // button1
                // 
                this->button1->Location = System::Drawing::Point(31, 32);
                this->button1->Name = L"button1";
                this->button1->Size = System::Drawing::Size(75, 27);
                this->button1->TabIndex = 0;
                this->button1->Text = L"button1";
                this->button1->UseVisualStyleBackColor = true;
                this->button1->Click += gcnew System::EventHandler(this,   &Form1::button1_Click);
                // 
                // 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->button1);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->ResumeLayout(false);

            }
    #pragma endregion
        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                     Form2^ frm = gcnew Form2;
            this->Controls->Add(frm->panel1);
             
                 }
        };
    }

コード形式 2:

    #pragma once

    namespace Windows_Forms_Application{

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

        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~Form2()
            {
                if (components)
                {
                    delete components;
                }
            }
        public: System::Windows::Forms::Panel^  panel1;
        protected: 

        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            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->panel1 = (gcnew System::Windows::Forms::Panel());
                this->SuspendLayout();
                // 
                // panel1
                // 
                this->panel1->BackColor =        System::Drawing::SystemColors::ActiveCaptionText;
                this->panel1->Location = System::Drawing::Point(106, 85);
                this->panel1->Name = L"panel1";
                this->panel1->Size = System::Drawing::Size(132, 118);
                this->panel1->TabIndex = 0;
                // 
                // Form2
                // 
                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->panel1);
                this->Name = L"Form2";
                this->Text = L"Form2";
                this->ResumeLayout(false);

            }
    #pragma endregion
        };
    }

コード cpp:

    // Windows Forms Application.cpp : main project file.

    #include "stdafx.h"
    #include "Form2.h"
    #include "Form1.h"

    using namespace WindowsFormsApplication;

    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {
        // Enabling Windows XP visual effects before any controls are created
        Application::EnableVisualStyles();
        Application::SetCompatibleTextRenderingDefault(false); 

        // Create the main window and run it
        Application::Run(gcnew Form1());
        return 0;
    }
4

1 に答える 1

0

クラスの前方宣言を使用してみてください。これが問題の最も可能性の高い原因です。

不明な場合の方法の例を次に示します ( C++ - 2 クラス 1 ファイル)。あなたの場合、直後に以下の行を追加してみてください

#include "stdafx.h"
#include "Form2.h"
class form2;

または、追加してみることができます

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

class form1;

これが機能しない場合は、コードとファイル構造をさらに確認する必要があります。
前方宣言は、コンパイラがコード内に存在する関数とオブジェクト、およびそれらへのアクセス方法を理解するのに役立つため、前方宣言が必要です。

于 2013-01-28T10:19:53.083 に答える