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!
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
#ifndef FORM2_H
#define FORM2_H
#pragma once
#include "Form1.h"
#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
The problem is because you have mutual inclusion of Form1.h in to Form2.h, and vice versa. The class definitions for each are thus not fully known before they are needed in the references within btnAddUser_Click and btnDone_Click. The solution is to define one or both of these out of line (i.e., in a standalone .cpp file).