【问题标题】:Pointer scope and constructor creation for array of struct结构数组的指针范围和构造函数创建
【发布时间】:2013-02-23 16:28:13
【问题描述】:

下面详述的问题 Jist:为什么我会收到错误提示 studentListindexPtr 未在此范围内声明?

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

现在我正在尝试修改我的程序并通过将所有信息发送到适当的函数来使其工作,但我不明白从我的主函数来回发送信息的正确方法。

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

#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++;
    }
}

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

编辑: 这是我的 main 函数的 sn-p,只是为了展示我如何调用 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);
        }

【问题讨论】:

  • 远不清楚(至少对我来说)你的实际问题是什么......
  • @MatsPetersson 我不知道为什么我会收到错误消息说studentListindexPtr 没有在这个范围内声明。我认为这是因为我的构造函数错误,但我不知道。

标签: c++ arrays constructor struct function-pointers


【解决方案1】:

在你的构造函数中:

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 是一个指针,所以你必须先间接它才能得到它指向的东西。

【讨论】:

  • 最初我在我的 struct 之后在我的课堂上有那行 studentEntry *studentList = new student[MAXSIZE];,但这没有用,我得到了我在课堂上无法拥有的错误
  • 我不遵循构造函数初始化程序语法,但在使用之前的建议时,我更改了代码以反映您的更改,但我仍然在该行出现错误,expected type-specifier before student。稍后在我的函数中,当我想使用由构造函数创建的数组时,我是使用student 还是studentList
  • @manalishi:我错过了你也在做new student而不是new studentEntry。我已经更新了我的答案。
  • 好吧,我想可能是这样。不幸的是,在 ';' 之前我仍然会收到一个预期 ']' 的错误。令牌...为什么会这样?
  • @manalishi:您的 MAXSIZE 定义也有问题。我已经更新了我的答案。
猜你喜欢
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
相关资源
最近更新 更多