2012-12-26 56 views
1

您好親愛的SO社區! 我有以下問題 - 我寫了使用結構簡單add_record功能(這是我的主文件):無法使用Windows窗體將操作添加到Visual Studio C++中的按鈕

// Exercise1.cpp : main project file. 

#include "stdafx.h" 
#include "Form1.h" 

using namespace Exercise1; 

typedef struct student { 
     char *name; 
     int index; 
     double avg; 
     student *next; 
     student *prev; 
} stud; 

student *first = 0; 

[STAThreadAttribute] 


void add_record(student **first, char *name, int index, double avg){ 
       student *new_stud = new student; 
       if (*first!=0) (*first)->prev = new_stud; 
       new_stud->name = name; 
       new_stud->avg = avg; 
       new_stud->index = index; 
       new_stud->next = *first; 
       new_stud->prev = 0; 
       *first = new_stud; 
} 

但是,我不能把這個記錄添加到功能按鈕操作(與預定義的數據,只是用於測試目的) 代碼Form1.h

#pragma once 

namespace Exercise1 { 

    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: System::Windows::Forms::Button^ button2; 
    //all remaining buttons here - irrelevant 

    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) 
     { 
} 
#pragma endregion 
    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { 
      } 
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
       String^i = "working"; 
       textBox1->Text = i; 

      } 
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { 
      String^g = "test"; 
      if(add_record(student **first, testname, 23, 3.5)) 
       textBox1->Text = g; 
     } 

}; 
} 

如何解決與記錄添加的那部分? 我收到了以下錯誤:

1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C2065: 'student' : undeclared identifier 
1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C2065: 'first' : undeclared identifier 
1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C2065: 'testname' : undeclared identifier 
1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C3861: 'add_record': identifier not found 

我想這是一些與變量聲明連接,但我不知道往哪裏放,爲了爲它工作..提前

感謝

+0

我想知道是誰投了票! – atoMerz

+0

@AtoMerZ - 嚴重的不是我......我不能在我自己的問題上投票 –

+1

您將不得不處理C++編譯器的侷限性,它是一次性編譯器。它必須先看到一個聲明,然後才能在代碼中使用該標識符。這將要求您將Form1方法定義寫入單獨的.cpp源代碼文件中。只要保留在Form1.h中的聲明,複製/粘貼方法體,例如Form1.cpp –

回答

0

來自你的代碼片段。

沒有定義爲student類型 - 該結構是typdefstud - 這解釋了爲什麼編譯罵你不承認在add_record方法聲明中student **first參數類型。

using namespace Exercise1; 

typedef struct student { 
     char *name; 
     int index; 
     double avg; 
     student *next; 
     student *prev; 
} stud; // <<<-------------------- should it be "} student;"? 

student *first = 0; 

[STAThreadAttribute] 


void add_record(student **first, char *name, int index, double avg){ 
       student *new_stud = new student; 
       if (*first!=0) (*first)->prev = new_stud; 
       new_stud->name = name; 
       new_stud->avg = avg; 
... 
相關問題