我正在使用2010。我有两个互相调用的窗口表单。Form1包括Form2
我读到我不能再次在Form1中包括Form2。而是使用ref类Form1。
但是我得到了以下错误
C:\user\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):error C2027:未定义类型'spl_project::Form1’1>的使用 C:\user\seuntech\documents\visual studio 2010\projects\spl_project\spl_project\Form2.h(11):请参阅'spl_project::Form1‘1>c:\users\seuntech\documents\visual studio 1>c:\users\seuntech\documents\visual C2227的声明:’->显示‘左侧’必须指向class/struct/union/generic 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 spl_project::Form1错误C2027:未定义类型'spl_project::Form1’1>的使用 C:\user\seuntech\documents\visual studio 2010\projects\spl_project\spl_project\Form2.h(11):请参阅'spl_project::Form1‘1>c:\users\seuntech\documents\visual studio 1>c:\users\seuntech\documents\visual错误C2227的声明:’->显示‘左’必须指向class/struct/union/泛型类型
代码如下
#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();
}
};}
发布于 2013-07-18 18:17:54
您必须将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);
}
}https://stackoverflow.com/questions/17729453
复制相似问题