为结构的数组创建指针作用域和构造函数



下面详细介绍了一个问题:为什么我会收到错误,说studentListindexPtr没有在这个范围内声明?

我编写了一个程序,该程序应该创建一个结构数组,并允许用户添加、删除和打印该数组中的项目。该程序运行良好,但我无法使添加、删除和打印功能单独正常工作。如果我的主函数中有所有的代码,那么一切都会完美地工作。

现在我正试图重新设计我的程序,并通过将所有信息发送到适当的函数来使其正常工作,但我不明白从我的主函数来回发送信息的正确方式。

这是我所有声明的头文件:

#include <iostream>
#include <string>
#define MAXSIZE 20;
using namespace std;
class Student
{
private:    
    struct studentEntry
    {
        string FIRST;
        string LAST;
        string ID;
        string CLASSIFICATION;
        string MAJOR;
    };
    int index;
public:
    void add(string &firstname, string &lastname, string &id, string &classification, string &major);
    void remove(string &id);
    void print(string &argument);
    int commandCompare(string &command);
    bool idCheck(string &id);
    Student();
};

然后这是我文件中的一些代码,其中包含了我所有的函数,也就是说,第一件事是我的学生类的构造函数。我试图做的是创建一个学生结构数组和一个指向索引的指针,这样我就可以在所有函数中使用相同的数组:

#include "students.h"
//constructor for Student class to create students array and pointer to index
Student::Student(){
    int *indexPtr = &index;
    studentEntry *studentList = new student[MAXSIZE];
}
//function to add entry to array of students
void Student::add(string &firstname, string &lastname, string &id, string &classification, string &major)
{
    string m_first = firstname; 
    string m_last = lastname;
    string m_id = id;
    string m_classification = classification;
    string m_major = major;
    int error = 0;
    //check if id is all digits, print error
    if (studentList[indexPtr]->idCheck(id) == false)
    {
        cout << "Error! ID can only contain digits." << endl;
        error = 1;
    }
    //search match for existing ids
    for(int i=0; i<indexPtr; i++)
    {
        //match found, print error, mark error true
        if(id.compare(studentList[i]->student->ID) == 0)
        {
            cout << "Error! ID already exists." << endl;
            error = 1;
        }   
    }
    else if(error != 1)
    {
        studentList[indexPtr]->FIRST = m_first;
        studentList[indexPtr]->LAST = m_last;
        studentList[indexPtr]->ID = m_id;
        studentList[indexPtr]->CLASSIFICATION = m_classification;
        studentList[indexPtr]->MAJOR = m_major;
        indexPtr = indexPtr++;
    }
}

我认为,一旦我解决了指针和数组的初始创建问题,程序中的一切都会正常工作。非常感谢您的帮助!

编辑:下面是我的主函数的一个片段,只是为了展示我是如何调用Student构造函数并尝试调用Student类中的函数的:

int main()
{
    Student students;
    string command;
    int quit = 0;
    //loop while user command != quit
    do
    {       
        cout << "students> ";
        cin >> command;
        //if user command = add
        if(students.commandCompare(command) == 1)
        {   
            string first, last, id, classification, major;
            cin >> first >> last >> id >> classification >> major;
            students.add(first, last, id, classification, major);
        }

在构造函数中:

Student::Student(){
    int *indexPtr = &index;
    studentEntry *studentList = new student[MAXSIZE];
}

这两条线毫无用处:

    int *indexPtr = &index;
    studentEntry *studentList = new studentEntry[MAXSIZE];

它们创建局部变量并为其赋值,但随后这些局部变量超出了范围。您也在执行new student而不是new studentEntry

我想你应该把studentList和indexPtr放在你的课上。

    int index;
    studentEntry *studentList;  // add this line
    int *indexPtr;              // add this line
public:
    void add(string &firstname, string &lastname, string &id, string &classification, string &major);

并像这样更改您的构造函数:

Student::Student(){
    indexPtr = &index;
    studentList = new studentEntry[MAXSIZE];
}

甚至更好——使用构造函数初始值设定项语法:

Student::Student()
  : indexPtr(&index),
    studentList(new studentEntry[MAXSIZE])
{
}

确保你定义了一个析构函数来释放内存:

~Student()
{
    delete [] studentList;
}

更好的是,如果使用std::vector,则可以完全避免手动分配内存。

另一个问题。在线:

#define MAXSIZE 20;

你多了一个分号。应该只是

#define MAXSIZE 20

如果你有分号,那么无论MAXSIZE在哪里使用,分号都会被放在代码中,所以这一行:

    studentList = new studentEntry[MAXSIZE];

将成为

    studentList = new studentEntry[20;];

这是一个语法错误。

另一个问题——像这样的行:

    studentList[indexPtr]->FIRST = m_first;

应该是这样的:

    studentList[*indexPtr].FIRST = m_first;

studentList是一个指针,但studentList[x]不是。此外,indexPtr是一个指针,因此必须先间接它才能得到它指向的内容。

最新更新