0

这是 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 来回切换,反之亦然。但这在显示对话框时会引发未定义类的错误,这可能是因为我包含了其中包含自身的东西。但是我需要包含才能再次调用该对话框以返回。

我应该怎么办?我需要使一个对话框可见,以便在 winform1 和 winform2 之间来回切换

提前致谢

4

1 回答 1

1

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

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


这就是我认为正在发生的事情:您已经在同一个文件中声明并实现了 Winform 1、2 和 3。当两个类都需要相互引用时,这会导致问题。查看此代码示例,其中两个类相互引用:

类1.h:

#pragma once
#include "Class2.h"
public ref class Class1
{
public:
    void Foo() { Class2^ two = gcnew Class2(); two->Bar() }
    void Baz() { }
}

类2.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 还没有被定义,所以你会得到一个编译器错误。

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

类1.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() { }

类2.h:

#pragma once
public ref class Class2
{
public:
    void Bar();
}

类2.cpp:

#include "Class2.h"
#include "Class1.h"

void Class2::Bar() { Class1^ one = gcnew Class1(); one->Baz() }
于 2012-04-15T16:25:40.330 回答