2013-07-18 44 views
0

我正在使用visual studio 2010.我有兩個互相調用的窗體窗體。 Form1包括Form2visual studio 2010 C++,包括windows窗體反覆使用

我讀過,我不能在Form2中再次包含Form1。而是使用ref類Form1。

,但我發現了以下錯誤

C:\用戶\ seuntech \文檔\ Visual工作室 2010 \項目\ spl_project \ spl_project \ Form2.h(85):錯誤C2512: 「spl_project :: Form1':沒有適當的默認構造函數可用 1> c:\ users \ seuntech \ documents \ visual studio 2010 \ projects \ spl_project \ spl_project \ Form2.h(86):錯誤C2027:使用 未定義類型'spl_project :: Form1'1>
c:\ users \ seuntech \ documents \ visual studio 2010 \ projects \ spl_project \ spl_project \ F orm2.h(11):請參閱 'spl_project :: Form1'1> c:\ users \ seuntech \ documents \ visual studio 2010 \ projects \ spl_project \ spl_project \ Form2.h(86)的聲明:錯誤C2227:left ' - > Show'必須指向class/struct/union/generic type 1> spl_project.cpp 1> c:\ users \ seuntech \ documents \ visual studio 2010 \ projects \ spl_project \ spl_project \ Form2.h( 85):error C2512: 'spl_project :: Form1':沒有適當的默認構造函數可用 1> c:\ users \ seuntech \ documents \ visual studio 2010 \ projects \ spl_project \ spl_project \ Form2.h(86):error C2027:使用 未定義類型'spl_project :: Form1'1>
c:\ users \ seuntech \ documents \ visual studio 2010 \ projects \ spl_project \ spl_project \ Form2.h(11):請參閱 'spl_project :: Form1'1> c:\ users \ seuntech \ documents \ visual studio 2010 \ projects \ spl_project \ spl_project \ Form2.h 86):錯誤C2227:左 ' - >顯示' 必須指向類/結構/聯合/通用型

代碼如下

#pragma once 
namespace spl_project { 

using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 

ref class Form1; 
// 
/// <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^ button1; 
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->button1 = (gcnew System::Windows::Forms::Button()); 
     this->SuspendLayout(); 
     // 
     // button1 
     // 
     this->button1->Location = System::Drawing::Point(176, 205); 
     this->button1->Name = L"button1"; 
     this->button1->Size = System::Drawing::Size(75, 23); 
     this->button1->TabIndex = 0; 
     this->button1->Text = L"button2"; 
     this->button1->UseVisualStyleBackColor = true; 
     this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click); 
     // 
     // Form2 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(284, 262); 
     this->Controls->Add(this->button1); 
     this->Name = L"Form2"; 
     this->Text = L"Form2"; 
     this->ResumeLayout(false); 



    } 
#pragma endregion 
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 

      Form1^na2 = gcnew Form1(); 
      na2->Show(); 

     } 
}; 

}

回答

1

您必須單獨的從聲明中執行button1_Click

所以放在 「Form2.h」 只就 「的button1_Click」 的宣言:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e); 

而且還去掉 「Form1的」 在 「Form2.h」 的 「formward定義」

現在創建一個新的文件Form1.cpp現在包含了實現,這其中也包括了「Forms2.h」:

這是「Form2.cpp」的內容:

#include "Form2.h" 
#include "Form1.h" 

namespace Form2::spl_project { 
    System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
    Form1^na2 = gcnew Form1(); 
    na2->ShowDialog(this); 
    } 
} 
+0

好的,我會試試看 –

相關問題