增加类指针数组
Increasing class pointer array
在我的代码中,我想在我的类指针阵列中添加一个学生信息,每次添加新学生时,阵列大小都会增加。这是我的代码:我的标题文件:
class Student{
public:
int studentID;
char studentName[20];
int currentEnrollment;
Student();
void AddStudent(Student *tempStudent[], int countStudent, int sizeOfArray);}
我的类定义文件:
void Student::AddStudent(Student *tempStudent[], int countStudent, int sizeOfArray)
{
for (int i = countStudent; i < sizeOfArray; i++)
{
cout << "Please enter student id (4 digits only): ";
cin >> tempStudent[i]->studentID;
cout << "Please enter student name: ";
cin >> tempStudent[i]->studentName;
}
}
我的main.cpp文件
int *totalStudent = new int;
*totalStudent = 1;
int i, j, countStudent = 0;
int sizeOfArray = *totalStudent;
Student *newStudent[*totalStudent];
//Each time a new student is added, I will allocate a new memory for the array element, then add student Info using function.
for (i = countStudent; i < *totalStudent; i++)
{
newStudent[i] = new Student;
newStudent[i]->AddStudent(newStudent, countStudent, sizeOfArray);
countStudent++;
*totalStudent++;
}
运行代码时,我会收到一个未定义的参考错误,因此我不知道我是否能够增加数组。我打算使用C 语法,因此我使用新的和删除。感谢您的帮助。P.S:这是我的新代码,它运行良好,唯一缺少的是数组中第一个元素的学生ID。在我的主要课程中:
int numStudent = 0;
int i, j, countStudent = 1;
Student *newStudent = new Student[countStudent];
AddStudent(newStudent, countStudent, numStudent);
我的学生.h
class Student{
public:
int studentID;
char studentName[20];
int currentEnrollment;
};
Student AddStudent(Student *newStudent, int &countStudent, int &numStudent);
和我的学生.cpp
Student AddStudent(Student *newStudent, int &countStudent, int &numStudent)
{
Student tempStudent;
cout << "Please enter student id (4 digits only): ";
cin >> tempStudent.studentID;
cout << "Please enter student name: ";
cin >> tempStudent.studentName;
newStudent[numStudent] = tempStudent;
numStudent++;
if (numStudent == countStudent)
{
Student *newStudentSize = new Student[countStudent + 1];
for (int i = 0; i < numStudent; i++)
{
newStudentSize[i] = newStudent[i];
}
delete []newStudent;
return *newStudentSize;
countStudent += 1;
}
}
运行此代码将为我带来以下结果:
StudentID: 11
StudentName: Dat
StudentID: 23
StudentName: Michael
Printing:
StudentID: 0
StudentName: Dat
StudentID: 23
StudentName: Michael
虽然增加每个新学生的数组(效率低下)是没有意义的,这是您可以做到的一种方法(我什至没有尝试编译您的代码,因为它有许多问题,并且不必要地复杂)。请注意,tempStudent
(在下面的代码段中)甚至不必使用new
创建。该解决方案将Student
对象存储在学生数组中(尽管可以将其修改为存储Student
对象指针的修改很容易)。就是说,通常,您只想创建一个足够大的数组来适应所有学生(只需将studentCount
设置为适当的数字,而不是以下示例中的1个)。
class Student{
public:
int studentID;
char studentName[20];
int currentEnrollment;
Student(){};
};
int main(){
int studentCount=1;
Student * students = new Student[studentCount];
int numStudents=0;
bool done=false;
char finished='N';
while (!done){
//Student *tempStudent = new Student();
//create a Student on the stack
Student tempStudent;
cout << "Please enter student id (4 digits only): ";
//No input checking is done here
cin >> tempStudent.studentID;
No input checking is done here
cout << "Please enter student name: ";
cin >> tempStudent.studentName;
students[numStudents] = tempStudent;
numStudents++;
cout << "Stop entering students: Y or N";
cin >> finished;
done = finished=='Y' or finished=='y' ? true : false;
if(numStudents==studentCount){
students = ReallocateStudents(students, studentCount, studentCount*2);
studentCount *= 2;
}
}//end while
//print the students info
for(int i=0;i<numStudents;i++){
Student st = students[i];
cout << st.studentID << " " << st.studentName << std::endl;
}
//deallocate the students array or if you place this in the main like you did, the program will exit immediately so there's no need to deallocate
return 0;
}
Student * ReallocateStudents(Student* st, int oldSize, int newSize){
Student * newStudents = new Student[newSize];
//copy the existing values from the old array to the new one
for(int i=0;i<oldSize;i++){
newStudents[i] = st[i];
}
delete [] st; //delete the old array
return newStudents;
}
更新:
由于您不想在main()
中做任何事情,只需创建一个免费的AddStudents
功能,然后在此处执行所有操作。或者,您可以创建一个 学生课内的静态功能。创建AddStudent
作为Student
的成员是没有意义的,因为这需要您使用学生实例添加新实例,这使设计差(更不用说技术问题等)。
int main(){
// some code here
Students * allStudents = AddStudents();
//print students
}//end main
Students * AddStudents(){
int studentCount=1;
Student * students = new Student[studentCount];
int numStudents=0;
bool done=false;
char finished='N';
while (!done){
//create a Student on the stack
Student tempStudent;
cout << "Please enter student id (4 digits only): ";
//No input checking is done here
cin >> tempStudent.studentID;
No input checking is done here
cout << "Please enter student name: ";
cin >> tempStudent.studentName;
students[numStudents] = tempStudent;
numStudents++;
cout << "Stop entering students: Y or N";
cin >> finished;
done = finished=='Y' or finished=='y' ? true : false;
if(numStudents==studentCount){
students = ReallocateStudents(students, studentCount,
studentCount*2);
studentCount *= 2;
}
}//end while
return students;
}
这很容易理解和维护,因此我建议使用这种方法。
addStudent
对其所属的 Student
对象无能为力。因此,无需将其放入"学生"课上。(或者您宁愿将其重写,以便与它所属的学生对象一起做某事)。目前,它没有"添加"任何东西,因此名称令人困惑。
从技术上讲是什么问题取决于您想要它做什么。当前,它初始化了预期已经存在并用一个数组(从特定数组索引到数组末尾)指向的学生对象。如果您想做的话,那很可能是一个有用的功能。但是,您必须用一系列指向有效的Student
对象正确调用,目前却没有。
目前,您有一个循环,可以在数组中初始化指针。每当您初始化指针时,您都会调用AddStudent(..)。问题是" AddStudent()"试图初始化您的数组指向的所有学生。
这有两个主要问题(除了您的循环中所有其他问题)。
-
每次创建新学生时,所有现有的学生都会 再次以STD :: CIN的新输入初始化。(因此,对于N学生,您将 尝试进行n*n初始化)
-
当
main
中的循环运行时,并非阵列中的所有指针 到现有的Student
对象。这可能导致重要数据是 覆盖,程序崩溃或完全不同且出乎意料的东西。
您应该坐下来重新评估自己想做的事情。尝试在现有代码中修复单个错误,一个接一个地将创建更多错误。
只是一个提示,可以让您入门:
class Student
{
public:
int studentID;
char studentName[20];
int currentEnrollment;
Student();
void init_from_cin();
};
和您的类定义文件中:
void Student::init_from_cin()
{
cout << "Please enter student id (4 digits only): ";
cin >> studentID;
cout << "Please enter student name: ";
cin >> studentName;
}
如果您创建一个新的Student
:
Student *new_student = new Student;
new_student->init_from_cin();
然后在调用init_from_cin()
后,应初始化new_student
指向的Student
对象。
如何在循环中创建和初始化多个Student
对象,作为读者的练习。但是,当您这样做时,您应该了解循环的下层和上限应该是什么。而且,您还应该理解为什么在循环运行时将上限移开是一个坏主意。
,永远不要忘记理智的编程语言开始以0。
- 添加到数组指针
- C++语法差异:二维和一维数组(指针算术)
- 数组指针表示法C++(移动数组时)
- 复制后删除原始数组指针将前 3 个字节设置为 0
- C++访问指向结构的指针中的类数组指针
- C++编译时使用 constexpr 字符数组指针分配静态数组?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- 创建<int>对整数数组指针的矢量引用 (C++)
- 将 2D 数组指针传递给 C++ 中的函数
- 创建指针是否超过非数组指针的末尾,而不是从 C++17 中的一元运算符和未定义的行为派生?
- 队列数组指针 (C++)
- C++数组指针上的删除操作
- 对于循环不循环和检测字符数组 [指针和字符数组]
- 如何初始化数组指针对象
- 如何正确传递 2D 数组指针作为参数
- 从数组指针中获取怪异的数字
- 初始化std :: unique_ptr作为原始数组指针的初始化
- 将结构数组指针从C#传递到C
- STD :: Sort将数组指针设置为NULL
- C++数组指针错误无法将“int*”转换为“int**”