Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为结构数组创建指针作用域和构造函数_C++_Arrays_Constructor_Struct_Function Pointers - Fatal编程技术网

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

C++ 为结构数组创建指针作用域和构造函数,c++,arrays,constructor,struct,function-pointers,C++,Arrays,Constructor,Struct,Function Pointers,下面详细说明的问题:为什么我会收到错误,说studentList和indexPtr未在此范围内声明 我编写了一个程序,该程序应该创建一个结构数组,并允许用户在该数组中添加、删除和打印项目。该程序运行良好,但我无法让添加、删除和打印功能自行正常工作。如果我把所有的代码都放在主函数中,那么一切都会完美地工作 现在,我正试图重新编写我的程序,并通过将所有信息发送到适当的函数使其正常工作,但我不了解从主函数来回发送信息的正确方法 这是我所有声明的头文件: #include <iostream>

下面详细说明的问题:为什么我会收到错误,说
studentList
indexPtr
未在此范围内声明

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

现在,我正试图重新编写我的程序,并通过将所有信息发送到适当的函数使其正常工作,但我不了解从主函数来回发送信息的正确方法

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

#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();
};
#包括
#包括
#定义最大尺寸20;
使用名称空间std;
班级学生
{
私人:
结构学生中心
{
先串;
最后一串;
字符串ID;
字符串分类;
弦乐大调;
};
整数指数;
公众:
void add(string&firstname、string&lastname、string&id、string&classification、string&major);
无效删除(字符串和id);
无效打印(字符串和参数);
int命令比较(字符串和命令);
布尔id检查(字符串和id);
学生();
};
这是我文件中的一些代码,里面有我所有的函数,也就是说,第一件事是我的学生类的构造函数。我试图做的是创建一个学生结构数组和一个指向索引的指针,这样我就可以在所有函数中使用相同的数组:

#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++;
    }
}
#包括“students.h”
//学生类的构造函数,用于创建学生数组和指向索引的指针
学生::学生(){
int*indexPtr=&index;
studentEntry*studentList=新学生[MAXSIZE];
}
//函数将条目添加到学生数组中
void Student::add(string&firstname、string&lastname、string&id、string&classification、string&major)
{
字符串m_first=firstname;
字符串m_last=lastname;
字符串m_id=id;
字符串m_分类=分类;
字符串m_major=major;
整数误差=0;
//检查id是否为所有数字,打印错误
如果(学生列表[indexPtr]->idCheck(id)==false)
{
cout ID=m_ID;
学生名单[indexPtr]->分类=m_分类;
学生名单[indexPtr]->MARY=m_MARY;
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);
        }
intmain()
{
学生;
字符串命令;
int-quit=0;
//用户命令时循环!=退出
做
{       
cout>命令;
//如果用户命令=添加
if(students.commandCompare(command)==1)
{   
字符串开头、结尾、id、分类、主要;
cin>>第一个>>最后一个>>id>>分类>>专业;
添加(第一、最后、id、分类、专业);
}
在构造函数中:

Student::Student(){
    int *indexPtr = &index;
    studentEntry *studentList = new student[MAXSIZE];
}
这两条线没有用:

    int *indexPtr = &index;
    studentEntry *studentList = new studentEntry[MAXSIZE];
他们创建局部变量并给它们赋值,但是这些局部变量超出了范围。你也在做
newstudent
,而不是
newstudentry

我想你是想把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];
}
    studentList[indexPtr]->FIRST = m_first;
    studentList[*indexPtr].FIRST = m_first;
更好的方法是使用构造函数初始值设定项语法:

Student::Student()
  : indexPtr(&index),
    studentList(new studentEntry[MAXSIZE])
{
}
并确保定义析构函数以释放内存:

~Student()
{
    delete [] studentList;
}
更好的是,如果使用std::vector,就可以完全避免手动分配内存

另一个问题。在这方面:

#define MAXSIZE 20;
    studentList = new studentEntry[MAXSIZE];
你有一个额外的分号。它应该是

#define MAXSIZE 20
如果分号在那里,那么无论使用MAXSIZE,分号都会放在代码中,因此此行:

#define MAXSIZE 20;
    studentList = new studentEntry[MAXSIZE];
将成为

    studentList = new studentEntry[20;];
这是一个语法错误

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

Student::Student(){
    indexPtr = &index;
    studentList = new studentEntry[MAXSIZE];
}
    studentList[indexPtr]->FIRST = m_first;
    studentList[*indexPtr].FIRST = m_first;
应该是这样的:

Student::Student(){
    indexPtr = &index;
    studentList = new studentEntry[MAXSIZE];
}
    studentList[indexPtr]->FIRST = m_first;
    studentList[*indexPtr].FIRST = m_first;

studentList
是一个指针,但
studentList[x]
不是。而且,
indexPtr
是一个指针,所以你必须先间接地将它指向它。

它还很不清楚(至少对我来说)你的实际问题是…@MatstPetersson我不知道为什么会出现错误,说
studentList
indexPtr
未在此范围内声明。我假设这是因为我的构造函数做错了,但我不知道。最初我有那行
Studentry*studentList=新学生[MAXSIZE];
在我的类中,就在我的结构之后,但这不起作用,我得到了一个错误,在我的类中,我不遵循构造函数初始值设定项语法,但在之前的建议下,我修改了我的代码以反映您的更改,我仍然在那一行上得到错误,
在学生之前需要类型说明符
。稍后在我的函数中,当我想使用构造函数创建的数组时,我是使用
student
还是
studentList
?@manalishi:我错过了你也在做
newstudent
,而不是
newstudentry
。我已经更新了我的答案。好吧,我想可能是这样。不幸的是,我仍然在做获取一个错误,该错误应在“;”标记之前出现“]”……为什么会出现这种情况?@manalishi:您的MAXSIZE定义也有问题。我已更新了答案。