Reputation: 2250
This is the Winform1:
#include "Winform2.h"
#include "Winform3.h"
namespace Winform1 {
/// <summary>
/// Summary for Winform1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Winform1: public System::Windows::Forms::Form
{
public:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Button^ button4;
public:
unordered_map<int, std::string>* fb_contas_email;
Usuario* usuario;
WinForm1(Usuario* user,
unordered_map<int, std::string>* fb_contas_)
{
this->fb_contas_email = fb_contas_;
this->usuario = user;
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->Visible = false;
this->Close();
WinForm2 wf2(this->usuario);
wf2.ShowDialog();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
this->Visible = false;
this->Close();
WinForm3 wf3(this->usuario);
wf3.ShowDialog();
}
//...
This is the Winform2:
#include "Winform1.h"
namespace Winform2 {
/// <summary>
/// Summary for Winform2
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Winform2: public System::Windows::Forms::Form
{
public:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Button^ button4;
public:
unordered_map<int, std::string>* fb_contas_email;
Usuario* usuario;
WinForm2(Usuario* user,
unordered_map<int, std::string>* fb_contas_)
{
this->fb_contas_email = fb_contas_;
this->usuario = user;
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
this->Visible = false;
this->Close();
WinForm1 wf1(this->usuario);
wf1.ShowDialog();
}
//...
This is the Winforms 3:
namespace Winform3 {
/// <summary>
/// Summary for Winform3
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Winform3: public System::Windows::Forms::Form
{
public:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Button^ button4;
public:
unordered_map<int, std::string>* fb_contas_email;
Usuario* usuario;
WinForm3(Usuario* user,
unordered_map<int, std::string>* fb_contas_)
{
this->fb_contas_email = fb_contas_;
this->usuario = user;
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
//...
How do I solve this error? I need to go back and forth from the Winform1 to the Winform2 and vice-versa. But this throws an error of undefined class when showing dialog probably because I'm including something that has itself in it. But I need the include in order to call that Dialog again to come back.
What should I do? I need to make a dialog visible to go back and forth between winform1 and winform2
Thanks in advance
Upvotes: 0
Views: 98
Reputation: 27864
Are these code samples the code (.cpp) files, or header files?
It looks to me like you need to do a C++ style separation of header files and code files. Take your code, and separate it into class definitions only in the header files, class implementations only in the .cpp files, and this compiler error will go away.
Here's what I think is going on: You've got Winform 1, 2, and 3 each declared & implemented in the same file. This causes a problem when two classes each need to reference each other. Look at this code sample, where two classes each reference each other:
Class1.h:
#pragma once
#include "Class2.h"
public ref class Class1
{
public:
void Foo() { Class2^ two = gcnew Class2(); two->Bar() }
void Baz() { }
}
Class2.h:
#pragma once
#include "Class1.h"
public ref class Class2
{
public:
void Bar() { Class1^ one = gcnew Class1(); one->Baz() }
}
Now, that's going to happen when these two files are compiled? Class1.h will attempt to include Class2.h. Class2.h will attempt to include Class1.h, but it won't because of the #pragma once
. (I'm assuming you have either that or an #ifdef guard in your header files.) When it attempts to compile, it'll see the definition of Class2, followed by the definition of Class1. When it compiles CLass2, Class1 hasn't been defined yet, so you'll get a compiler error.
The solution here is to separate each of the classes into separate header and code files. See fixed up example:
Class1.h:
#pragma once
public ref class Class1
{
public:
void Foo();
void Baz();
}
Class1.cpp:
#include "Class1.h"
#include "Class2.h"
void Class1::Foo() { Class2^ two = gcnew Class2(); two->Bar() }
void Class1::Baz() { }
Class2.h:
#pragma once
public ref class Class2
{
public:
void Bar();
}
Class2.cpp:
#include "Class2.h"
#include "Class1.h"
void Class2::Bar() { Class1^ one = gcnew Class1(); one->Baz() }
Upvotes: 1