0

我正在使用 Visual Studio 2010。我有两个相互调用的窗口窗体。Form1 包含 Form2

我读到我不能再次在 Form2 中包含 Form1。而是使用 ref 类 Form1。

但我收到以下错误

c:\users\seuntech\documents\visual studio 2010\projects\spl_project\spl_project\Form2.h(85): error C2512: 'spl_project::Form1' : 没有合适的默认构造函数可用 1>c:\users\seuntech\文档\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\Form2.h(11) :参见 'spl_project::Form1' 1>c:\users\seuntech\documents\visual studio 2010\ 的声明projects\spl_project\spl_project\Form2.h(86): 错误 C2227: '->Show' 左侧必须指向类/结构/联合/通用类型 1> spl_project.cpp 1>c:\users\seuntech\documents\ visual studio 2010\projects\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\Form2.h(11) :参见 'spl_project::Form1' 1>c:\users\seuntech\documents\visual studio 2010\ 的声明projects\spl_project\spl_project\Form2.h(86): 错误 C2227: '->Show' 左侧必须指向类/结构/联合/通用类型

代码如下

 #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();

         }
};

}

4

1 回答 1

1

您必须将执行button1_Click与声明分开。

所以在“Form2.h”中只放入关于“button1_Click”的声明:

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

并且还删除了“Form2.h”中“Form1”的“前向定义”

现在创建一个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);
  }
}
于 2013-07-18T18:17:54.117 回答