2016-04-02 2 views
0

Я знаю, что есть много сообщений по этой теме, но все же я не могу заставить его работать. Я создал новый проект под названием «nis».visual studio C++ forms multiple includes

Это "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; 
} 

У меня есть две формы. Это "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 

И это "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 

Можете ли вы сказать мне, что случилось с охранниками включения? я получаю эту ошибку:

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 

Спасибо так много!

+0

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

ответ

0

Вы включаете форму 1 в форму 2 и форму 2 в форму 1. Что вы ожидаете от препроцессора? Вы должны сделать выбор здесь! В общем, модули должны формировать иерархию, а не сеть, и, конечно, не график с циклами, что означает, что модуль в конце включает себя.

Если вы хотите взаимно ссылаться между формой 1 и формой 2, не помещайте весь свой код в заголовки, а делайте отдельные файлы реализации. Ваши заголовки будут содержать только объявления вашего класса. Сопровождающие файлы C++ будут содержать код методов, включая их ссылки на другую форму.

+0

OK Я знаю это, но можете ли вы сказать мне, что такое решение? Я хочу, чтобы опция перемещалась от «Form1» до «Form2» и наоборот. Я думал, что использование «#ifndef» должно решить рекурсию. – user1074988

+0

Привет. Я сожалею, но C++ для меня новичок, я просто не знаю, как получить это решение и применить его в своих формах. Не могли бы вы мне помочь? – user1074988

+0

В следующем примере показано, как разделить заголовок и реализацию: http://p2p.wrox.com/c-programming/92954-c-class-example-separate-header-implementation-file.html Кстати, чтобы быть честный, C++ - это не самый простой способ работать с Microsoft .net (это то, что вы делаете). C# является более популярным для этой области, а IronPython еще проще. Сказав это, изучение C++ - это очень хорошая и долговременная инвестиция, поскольку вы можете сделать многое с ним, а затем программировать для MS .net. –

Смежные вопросы