0
votes

Accessing (adding) an element of Form2 to Form1 works just fine with c++ vs2010. If I try the same thing from scratch in visual studio express 2012, I keep getting an error saying Form2 is undeclared identifier. Any ideas what is wrong?

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

error C2065: 'Form2' : undeclared identifier

Code Form1:

include "stdafx.h"

include "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);
             
                 }
        };
    }

Code Form2:

    #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
        };
    }

Code 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;
    }
1
Show the code of Form2.h please.Leo Chapiro

1 Answers

0
votes

Try using a class forward-declaration. This is the most likely cause of your problem.

Here an example of how to do it if your unsure (C++ - 2 classes 1 file). In your case try adding the the line below just after

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

Alternatively you could try adding

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;

If this doesn't work we'll need to see more of your code and file structure.
You need forward declarations because it aids the compiler in understanding what functions and objects exist in your code and how to access them.