2012-04-13 56 views
0

這時引發未定義類錯誤是Winform1:實例化WinForm2從WinForm1被添加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提前

感謝

回答

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