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?

7
  • 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. Commented May 13, 2020 at 4: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? Commented May 13, 2020 at 4: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? Commented May 13, 2020 at 4: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. Commented May 13, 2020 at 4:31
  • 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 (written in terms of pointers instead of array notation, but the principles still hold). Commented May 13, 2020 at 5:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.