So, i have a simple forms application : i have a form 1 (Form1.h) and a second f
ID: 654608 • Letter: S
Question
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
#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
Explanation / Answer
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.