2013-11-22 165 views
-3

我試圖讓Windows形成,這是Form1.h:轉換的Win32控制檯應用程序Windows窗體應用程序

#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) { 
     } 
}; 
} 

和頭文件我已經添加了頭文件student.h並寫班級學生它: 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'&'

錯誤2錯誤C2662:'student :: setStudent':無法將'this'指針從'student'轉換爲'student &「

錯誤1錯誤C4368:無法定義的」作爲管理的一員‘formApp :: Form1的’:混合型,不支持

請你能告訴我正確的方式來增加學生類到Windows窗體應用程序並使用它。

回答

0

正如您現在定義的那樣,student是一個非託管類,Form1是一個託管類。託管類不允許將非託管類作爲內聯字段。您可以通過以下兩種方式之一解決這個問題:

  1. s指針類型(student*)來代替,並建立在Form1的構造一個新的對象。
  2. (首選)將student更改爲託管類別(ref class student),並使s爲受控句柄(student^)。

現在,這就是說,如果您嘗試在C++/CLI中編寫此代碼,您將面臨許多令人頭疼的問題。請考慮切換到C#,這將使WinForms開發變得更容易。

相關問題