Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.
  1. So, i have a simple forms application : i have a form 1 (Form1.h) and a second form (Form2.h). Form1 consists of a simple Add User button(which at this stage does nothing but opens the form2 and hides form1).(Till this point , everything works).
    But when I add a button "Done" to the Form2 which is supposed to open form1 again , there are following errors :
    left of ->Show must point to class/struct/union/generic type
    f1: undeclared identifier
    Form1: undeclared identifier
    Please help!

  2. the code is :

    //Project Name: MultipleForms
    //Form1.h
    #ifndef FORM1_H
    #define FORM1_H
    #pragma once
    #include "Form2.h"
    namespace MultipleForms {
    
        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^  btnAddUser;
        private: System::Windows::Forms::Button^  btnExit;
        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->btnAddUser = (gcnew System::Windows::Forms::Button());
                this->btnExit = (gcnew System::Windows::Forms::Button());
                this->SuspendLayout();
                // 
                // btnAddUser
                // 
                this->btnAddUser->Location = System::Drawing::Point(78, 44);
                this->btnAddUser->Name = L"btnAddUser";
                this->btnAddUser->Size = System::Drawing::Size(75, 23);
                this->btnAddUser->TabIndex = 0;
                this->btnAddUser->Text = L"Add User";
                this->btnAddUser->UseVisualStyleBackColor = true;
                this->btnAddUser->Click += gcnew System::EventHandler(this, &Form1::btnAddUser_Click);
                // 
                // btnExit
                // 
                this->btnExit->Location = System::Drawing::Point(263, 136);
                this->btnExit->Name = L"btnExit";
                this->btnExit->Size = System::Drawing::Size(75, 23);
                this->btnExit->TabIndex = 1;
                this->btnExit->Text = L"Exit";
                this->btnExit->UseVisualStyleBackColor = true;
                this->btnExit->Click += gcnew System::EventHandler(this, &Form1::btnExit_Click);
                // 
                // Form1
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(410, 200);
                this->Controls->Add(this->btnExit);
                this->Controls->Add(this->btnAddUser);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->ResumeLayout(false);
    
            }
    #pragma endregion
        private: System::Void btnAddUser_Click(System::Object^  sender, System::EventArgs^  e) {
                     Form2^ f2 = gcnew Form2();
                     f2->Show();
                     this->Hide();
                 }
        private: System::Void btnExit_Click(System::Object^  sender, System::EventArgs^  e) {
                     exit(1);
                 }
        };
    }
    
    #endif
    
    //Form2.h
    
    #include "Form1.h"
    #ifndef FORM2_H
    #define FORM2_H
    #pragma once
    
    #include<stdlib.h>
    
    namespace MultipleForms {
    
        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;
                }
            }
        private: System::Windows::Forms::Button^  btnDone;
        protected: 
    
        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->btnDone = (gcnew System::Windows::Forms::Button());
                this->SuspendLayout();
                // 
                // btnDone
                // 
                this->btnDone->Location = System::Drawing::Point(98, 110);
                this->btnDone->Name = L"btnDone";
                this->btnDone->Size = System::Drawing::Size(75, 23);
                this->btnDone->TabIndex = 0;
                this->btnDone->Text = L"Done";
                this->btnDone->UseVisualStyleBackColor = true;
                this->btnDone->Click += gcnew System::EventHandler(this, &Form2::btnDone_Click);
                // 
                // Form2
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(292, 273);
                this->Controls->Add(this->btnDone);
                this->Name = L"Form2";
                this->Text = L"Form2";
                this->ResumeLayout(false);
    
            }
    #pragma endregion
        private: System::Void btnDone_Click(System::Object^  sender, System::EventArgs^  e) {
                     Form1^ f1 = gcnew Form1();
                     f1->Show();
                     this->Hide();
                 }
        };
    }
    #endif
    
share|improve this question

closed as off topic by Doc Brown, Bart van Ingen Schenau, MichaelT, GlenH7, World Engineer Apr 28 '13 at 1:34

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Questions of this kind are much better suited on stackoverflow.com. Expect you question to be moved automatically, when enough people have voted for that. –  Doc Brown Apr 27 '13 at 15:30
    
@DocBrown for future reference, how do I vote for that? I think this would be better on SO also. –  Victor Apr 27 '13 at 16:15
    
@Victor : regarding voting for a question, i think you have to click on the up arrow made right above the number placed to the left of the question(i am new to this site..so don't know exactly) –  mclaren Apr 27 '13 at 17:30
1  
Hello and Welcome to Programmers. Please don't crosspost between sites on the network. With regard to migration, unless you have 3000+ reputation you need to flag to migrate or close rather than vote. Please read the About page and the FAQ for more information. Have a pleasant day. –  World Engineer Apr 28 '13 at 1:34

1 Answer 1

up vote 1 down vote accepted

In Form1 the first thing it does is include Form2 (after the guard). In Form2 the first thing it does is include Form1, but your guard at the top of the file makes this a no-op. So Form1 "sees" Form2. From Form2 it includes Form1 (before the guard) which includes Form2 (the guard is below the include) which includes Form1 (before the guard of Form2 but it is ignored by its own guard) ending with Form2 followed by Form1. So starting from Form2 it doesn't know about Form1 until after your code with the error.

You should separate it into h and cpp files and include Form1 and Form2 in both cpp files so both of them know all the definitions.

share|improve this answer
    
thanks a lot sir..it worked –  mclaren Apr 27 '13 at 15:31
    
though the error remained if i included form2 and form1 both after the guard. But , seperating into .h and .cpp worked –  mclaren Apr 27 '13 at 15:33
    
@mclaren: technically, it may work, but have you noticed that f2 does not show the original Form1 object, instead it creates a new form (with completely new values) - I guess that is not what you want. –  Doc Brown Apr 27 '13 at 15:54
    
@mclaren @DocBrown I agree, it might be better to separate this into three objects or have Form1 pass itself to Form2 either during construction or as a property. –  Victor Apr 27 '13 at 16:14
    
@Victor Initially, as my code was'nt working, i tended to ignore that fact(new form being created), but it's true that i actually wish to pass the entire form1 to form2 . But i'm new to visual studio, so will have to research somewhat on how to do that.<br> Also,i will really appreciate if you guys help me in this! –  mclaren Apr 27 '13 at 17:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.