2016-04-02 13 views
0

Ich weiß, es gibt viele Beiträge zu diesem Thema, aber ich kann es immer noch nicht funktionieren. Ich habe ein neues Projekt namens "nis" erstellt.Visual Studio C++ bildet mehrere Einfügung

Das ist "nis.cpp":

// nis.cpp : main project file. 

#include "stdafx.h" 
#include "Form1.h" 

using namespace nis; 

[STAThreadAttribute] 
int main(array<System::String ^> ^args) 
{ 
// Enabling Windows XP visual effects before any controls are created 
Application::EnableVisualStyles(); 
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it 
Application::Run(gcnew Form1()); 
return 0; 
} 

ich zwei Formen haben. Das ist "Form1.h":

#ifndef FORM1_H 
#define FORM1_H 
#pragma once 
#include "Form2.h" 
namespace nis { 

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

/// <summary> 
/// Summary for Form1 
/// </summary> 
public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    Form1(void) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
    } 

protected: 
    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    ~Form1() 
    { 
     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(78, 77); 
     this->button1->Name = L"button1"; 
     this->button1->Size = System::Drawing::Size(144, 53); 
     this->button1->TabIndex = 0; 
     this->button1->Text = L"button1"; 
     this->button1->UseVisualStyleBackColor = true; 
     this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(282, 253); 
     this->Controls->Add(this->button1); 
     this->Name = L"Form1"; 
     this->Text = L"Form1"; 
     this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); 
     this->ResumeLayout(false); 

    } 
#pragma endregion 
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { 
     } 
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
      this->Hide(); 
      Form2^ f1 = gcnew Form2(); 
      f1->ShowDialog(); 
     } 
}; 
} 

#endif 

Und das ist "Form2.h":

#ifndef FORM2_H 
#define FORM2_H 
#pragma once 
#include "Form1.h" 
namespace nis { 

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

/// <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(44, 102); 
     this->button1->Name = L"button1"; 
     this->button1->Size = System::Drawing::Size(149, 53); 
     this->button1->TabIndex = 0; 
     this->button1->Text = L"button1"; 
     this->button1->UseVisualStyleBackColor = true; 
     // 
     // Form2 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(282, 253); 
     this->Controls->Add(this->button1); 
     this->Name = L"Form2"; 
     this->Text = L"Form2"; 
     this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load); 
     this->ResumeLayout(false); 

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

Können Sie mir bitte sagen, was mit den Einschluss Wachen falsch? Ich erhalte diese Fehler:

Error 1 error C2065: 'Form2' : undeclared identifier 
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85 
Error 2 error C2065: 'f1' : undeclared identifier 
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85 
Error 3 error C2061: syntax error : identifier 'Form2' 
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85 
Error 4 error C2065: 'f1' : undeclared identifier 
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 86 
Error 5 error C2227: left of '->ShowDialog' must point to 
class/struct/union/generic type d:\users\lior\documents\visual studio 
2010\projects\nis\nis\Form1.h 86 

Thank you so much!

+0

http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c- –

Antwort

0

Sie schließen Formular 1 in Formular 2 und Formular 2 in Formular 1 ein. Was erwarten Sie vom Präprozessor? Sie müssen hier eine Wahl treffen! Im Allgemeinen sollten Module eher eine Hierarchie als ein Netzwerk bilden, und schon gar kein Diagramm mit Zyklen, was bedeuten würde, dass sich ein Modul am Ende einfügt.

Wenn Sie sich gegenseitig zwischen Formular 1 und Formular 2 beziehen möchten, sollten Sie nicht den gesamten Code in die Header eingeben, sondern separate Implementierungsdateien erstellen. Ihre Header werden nur Ihre Klassen-Deklarationen enthalten. Die begleitenden C++ - Dateien enthalten den Code der Methoden, einschließlich ihrer Referenzen in das andere Formular.

+0

OK ich weiß das, aber können Sie mir sagen, was die Lösung ist? Ich möchte die Option von "Form1" zu "Form2" navigieren und umgekehrt. Ich dachte, dass die Verwendung von "#ifndef" die Rekursion lösen sollte. – user1074988

+0

Hallo. Es tut mir leid, aber C++ ist neu für mich, ich weiß einfach nicht, wie ich das lösen und in meine Formulare übernehmen kann. Kannst du mir bitte Helfen? – user1074988

+0

Das folgende Beispiel zeigt Ihnen, wie Sie Header und Implementierung trennen: http://p2p.wrox.com/c-programming/92954-c-class-example-separate-header-implementation-file.html By the way, zu sein Ehrlich, C++ ist nicht der einfachste Weg, mit Microsoft .net zu arbeiten (was Sie gerade tun). C# ist mehr Mainstream für diesen Bereich und IronPython ist noch einfacher. Allerdings ist das Lernen von C++ eine sehr gute und dauerhafte Investition, da Sie viel mehr damit anfangen können, als MS.NET zu programmieren. –