0

new to C++ for a school project and I cannot seem to get past this final part of my project.

I have a class "Roster" that has an array of object pointers

Student* classRoster[MAX_ROSTER] = {};

These "Student" objects have been dynamically added to the array with a Roster method that does:

classRoster[arrayLength++] = new Student(...);

Where

#define MAX_STUDENTS 5
     int arrayLength = 0;

The goal is to remove a specific student from the array but keep the others. The function looks something like this:

for (int i = 0; i < MAX_STUDENTS; i++) {
        if (classRoster[i]->getID() == studentID) {}
}

Now inside this function I have tried a number of different things, delete the memory and set the pointer to null, attempt to delete the memory and re-arrange the array, but nothing seems to work.

I found this question with an accepted answer: Set array of object to null in C++, but that isn't working for me and I cannot figure out why.

I have set a bool and position int in the function before the loop and tried removing the student after identification, removing in the loop etc.

I assumed this would be correct:

delete[] classRoster[i];
classRoster[i] = nullptr;

(Where i is the matched student) But this deletes the memory for all the elements in the array and if I just try

classRoster[i] = nullptr;

that makes all the elements after "i" also nullptr.

delete* classRoster[i];

gives an error that we cannot delete type 'Student'

and

delete classRoster[i];

does nothing since the array doesn't have objects but pointers to objects.

What am I doing wrong?

  • 3
    `delete classRoster[i];` should work. But you will also probably need to shift all the pointers after `i` left by one. Also, decrease `arrayLength` by one. – 001 May 13 '20 at 04:20
  • 1
    as @JohnnyMopp said. The reason why `delete *classRoster[i]` is not working is because the delete operator need to get a pointer to an object and not the object itself. Also, I see that the loop iterate until `MAX_STUDENTS`, what happens if you have less than `MAX_STUDENTS` in the array? – Enosh Cohen May 13 '20 at 04:27
  • @EnoshCohen I had not thought about the MAX_STUDENTS length. I could change that to an int and if the array is decreased, just decrease the int? What delete should be used here then? – Virtual Penman May 13 '20 at 04:31
  • @JohnnyMopp I just tried to implement a for loop where i is the position of the found student and then ```classRosterArray[i++] = classRosterArray[i]``` but nothing seems to happen. I am sure I am doing something wrong there. I thought maybe my order was off for assignment but ```classRosterArray[i] = classRosterArray[i++]``` just assigns the student to be removed, to the elements higher up. – Virtual Penman May 13 '20 at 04:31
  • I was able to get it by adjusting my for loop after the student was found. For some reason having ```classRosterArray[i] = classRosterArray[i++];``` did not work but ```classRosterArray[i] = classRosterArray[i+1];``` did. Also I was not adding in ```i--;``` after finding the student and performing the loop to shift the elements. Thanks guys! – Virtual Penman May 13 '20 at 04:57
  • 1
    @VirtualPenman Using both `i` and `i++` in the same statement tends to be confusing. I advise avoiding it, but if you must use it see [this answer](https://stackoverflow.com/a/61603919/9837301) (written in terms of pointers instead of array notation, but the principles still hold). – JaMiT May 13 '20 at 05:06
  • If you still want an explanation for the behavior you observed, we probably need a [mre]. There seems to be something going on that did not make it into your description. – JaMiT May 13 '20 at 05:15

0 Answers0