1

您好,亲爱的 SO 社区!我有以下问题 - 我使用结构编写了一个简单的 add_record 函数(这是我的主文件):

// 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

我想这与变量声明有关,但我不知道该把它放在哪里才能让它工作..

提前致谢

4

1 回答 1

0

从您的代码段。

没有类型定义为student- 结构是typdef-stud这解释了为什么编译器对你大喊它不识别方法声明student **first中的参数类型。add_record

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;
...
于 2014-07-29T11:39:40.430 回答