0

我知道有很多关于这个主题的帖子,但我仍然无法让它发挥作用。我创建了一个名为“nis”的新项目。

这是“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

太感谢了!

4

1 回答 1

0

您在表格 2 中包含表格 1,在表格 1 中包含表格 2。您希望预处理器做什么?你必须在这里做出选择!一般来说,模块应该形成一个层次结构而不是一个网络,当然也不是一个有循环的图,这意味着一个模块最终包含了它自己。

如果要在表格 1 和表格 2 之间相互引用,请不要将所有代码放在头文件中,而是制作单独的实现文件。您的标题将仅包含您的类 decls。随附的 C++ 文件将保存方法的代码,包括它们对另一种形式的引用。

于 2016-04-02T09:11:10.027 回答