为结构的数组创建指针作用域和构造函数
Pointer scope and constructor creation for array of struct
下面详细介绍了一个问题:为什么我会收到错误,说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();
};
然后这是我文件中的一些代码,其中包含了我所有的函数,也就是说,第一件事是我的学生类的构造函数。我试图做的是创建一个学生结构数组和一个指向索引的指针,这样我就可以在所有函数中使用相同的数组:
#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
是一个指针,因此必须先间接它才能得到它指向的内容。
- C++quit()函数中可能存在作用域问题
- 全局作用域中函数指针的赋值
- 在类函数中初始化外部作用域变量
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 是同一作用域的函数部分中的函数调用
- 在构造函数中输入对象时C++类成员作用域
- 在调用函数的作用域中构造返回的对象
- c++中的继承、作用域和模板构造函数
- 键入安全枚举和作用域辅助函数
- 被视为依赖作用域的函数模板的函数作用域结构体的静态成员函数
- 局部作用域和函数作用域的区别
- 在c++中,没有变量名的构造函数调用的生存期/作用域是什么?
- 从构造函数捕获异常意味着我的实例之后超出了作用域
- 在包含作用域的函数调用期间持续的临时函数参数
- 构造函数排序(全局作用域)问题
- 名称空间作用域构造函数定义是否需要类限定的标识符
- 为结构的数组创建指针作用域和构造函数
- 为什么基类构造函数没有被作用域解析操作符调用
- c++移动构造函数和作用域