c++ - 结构数组的指针范围和构造函数创建

标签 c++ arrays constructor struct function-pointers

下面详细说明了问题的要点:为什么我会收到错误消息,指出 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 放在您的类(class)中。

    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 是一个指针,因此您必须先间接它才能获得它指向的内容。

关于c++ - 结构数组的指针范围和构造函数创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042736/

相关文章:

C++ A星实现--判断节点是否已经在未清项优先队列中

c++ - 使用 erase 和 remove 从字符串中删除字符

c - c中的静态矩阵乘法到动态矩阵

java - String 类中的空构造函数

c++ - 在堆栈上构造之前的定义?类似于指针

c++ - 嵌套模板 typedef - 类型定义

c++ - 如何构建 C++ 应用程序以使用多核处理器

java - 使用 LinkedList 的输出比我预期的输出奇怪

javascript - 用于初始化 Javascript 数组数组的嵌套 For 循环

c# - 是否有可能在静态 ctor 完成运行之前创建一个类(常规 ctor)的实例(在多线程环境中)?