实例化WinForm2从WinForm1添加WinForm1实例化时抛出一个未定义的类错误

时间:2012-04-13 22:20:47

标签: visual-studio-2008 visual-c++ c++-cli

这是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();


         }
 //...

这是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();


         }
 //...

这是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
            //
        }
 //...

如何解决此错误?我需要从Winform1到Winform2来回,反之亦然。但是这会在显示对话框时抛出未定义类的错误,可能是因为我在其中包含了自己的内容。但我需要包含才能再次调用Dialog回来。

我该怎么办?我需要在winform1和winform2之间来回显示一个对话框

提前致谢

1 个答案:

答案 0 :(得分:1)

这些代码是代码(.cpp)文件还是头文件?

在我看来,您需要对头文件和代码文件进行C ++样式分离。获取代码,并将其分别只在头文件中,只在.cpp文件中的类实现中分类,并且此编译器错误将消失。


以下是我的想法:你已经宣布了Winform 1,2和3在同一个文件中实现。当两个类都需要相互引用时,这会导致问题。看看这个代码示例,其中两个类互相引用:

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() }
}

现在,当这两个文件被编译时会发生这种情况? Class1.h将尝试包含Class2.h。 Class2.h将尝试包含Class1.h,但由于#pragma once,它不会。 (我假设您在头文件中有或者#ifdef保护。)当它尝试编译时,它会看到Class2的定义,然后是Class1的定义。当它编译CLass2时,尚未定义Class1,因此您将收到编译器错误。

这里的解决方案是将每个类分成单独的头文件和代码文件。见固定示例:

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() }