我正在尝试制作Windows窗体,这是Form1.h:
#pragma once
#include"student.h"
namespace formApp {
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;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::TextBox^ textBox4;
private: System::Windows::Forms::TextBox^ textBox5;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
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->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->textBox4 = (gcnew System::Windows::Forms::TextBox());
this->textBox5 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(529, 209);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"max";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(262, 76);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 1;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(262, 132);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 2;
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(262, 181);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(100, 20);
this->textBox3->TabIndex = 3;
//
// textBox4
//
this->textBox4->Location = System::Drawing::Point(262, 37);
this->textBox4->Name = L"textBox4";
this->textBox4->Size = System::Drawing::Size(100, 20);
this->textBox4->TabIndex = 4;
//
// textBox5
//
this->textBox5->Location = System::Drawing::Point(529, 256);
this->textBox5->Name = L"textBox5";
this->textBox5->Size = System::Drawing::Size(100, 20);
this->textBox5->TabIndex = 5;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(206, 36);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(15, 13);
this->label1->TabIndex = 6;
this->label1->Text = L"id";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(198, 80);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(36, 13);
this->label2->TabIndex = 7;
this->label2->Text = L"mark1";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(209, 135);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(36, 13);
this->label3->TabIndex = 8;
this->label3->Text = L"mark2";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(201, 181);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(36, 13);
this->label4->TabIndex = 9;
this->label4->Text = L"mark3";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(745, 290);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox5);
this->Controls->Add(this->textBox4);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
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);
this->PerformLayout();
}
#pragma endregion
student s;
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int id= System::Convert::ToInt32(textBox1->Text);
int g1= System::Convert::ToInt32(textBox2->Text);
int g2= System::Convert::ToInt32(textBox3->Text);
int g3= System::Convert::ToInt32(textBox4->Text);
s.setStudent(id,g1,g2,g3);
textBox5->Text= System::Convert::ToString(s.maxGrade());
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
在Header Files上我添加了头文件student.h并在其中编写了class student: student.h:
#include<iostream>
#include<string>
using namespace std;
class student
{
int id;
int grade1,grade2,grade3;
public:
student()
{
}
void setStudent(int a,int b,int c,int d)
{
id=a;
grade1=b;
grade2=c;
grade3=d;
}
int maxGrade()
{
int max=grade1;
if(grade2>max)
max=grade2;
if(grade3>max)
{
max=grade3;
}
return max;
}
};
但编译器给了我错误:
错误3错误C2662:'student :: maxGrade':无法将'this'指针从'student'转换为'student&amp;'
错误2错误C2662:'student :: setStudent':无法将'this'指针从'student'转换为'student&amp;'
错误1错误C4368:无法将's'定义为托管'formApp :: Form1'的成员:不支持混合类型
请告诉我将学生类添加到Windows表单应用程序并使用它的正确方法。
答案 0 :(得分:0)
正如您现在定义的那样,student
是一个非托管类,Form1
是托管类。托管类不允许将非托管类作为内联字段。您可以通过以下两种方式之一解决此问题:
s
成为指针类型(student*
),并在Form1的构造函数中创建一个新对象。student
更改为托管类(ref class student
),并将s
设为托管句柄(student^
)。现在,如果您尝试在C ++ / CLI中编写此内容,那么您将面临许多麻烦。考虑切换到C#,它将使WinForms开发变得更容易。