Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vector of a class resetting member variables after for loop

I have an assignment where we need to use this basic structure of vectors and classes to learn about parent and child classes and polymorphism. Here is the code of the function I’m supposed to write:

void assignStudents(vector<Student*>& v) {
    for (int i = 0; i < 5; i++)
    {
        cout << "Enter a study level: ";
        string input;
        cin >> input;
        if (input == "graduate")
        {
            Graduate inputClass;
            Student* inputParentClassPtr = &inputClass;
            v.push_back(inputParentClassPtr);
            v[i]->addToVector(input);
            inputParentClassPtr = nullptr;

        }
        else if (input == "undergraduate")
        {
            Undergraduate inputClass;
            Student* inputParentClassPtr = &inputClass;
            inputParentClassPtr->addToVector(input);
            v.push_back(inputParentClassPtr);
        }
        else
        {
            cout << "Please enter a valid response, either graduate or undergraduate" << endl;
            i--;
        }

    }
    for (size_t i = 0; i < v.size(); i++)
    {
        vector<string> studyLevels = v[i]->getStudyLevels();
        size_t size = studyLevels.size();
        for (int j = 0; j < size; j++)
        {
            cout << studyLevels[j];
        }
    }
}

I debug the program and every time the first for loop moves on to the next iteration, every member variable inside each object in my vector goes blank, but then when I add a new object into the vector, then call the addToVector() function they come back.

I added the bottom for loop to check if any editing is happening, and once I get to that bottom for loop, every member variable is empty again.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have the Student class vector where I am adding Undergraduate and Graduate classes to. Every Student class has a protected vector inside called levels. I need to add the class to vector that holds all my objects, then edit the member variable vector to include the string representing the type of class it is.

Why do the member variables (levels) go blank every time it finishes an iteration of the for loop?

>Solution :

I’ll just focus on one part, as the same issues appear twice.

{
    Graduate inputClass; // create local student "on the stack"
    Student* inputParentClassPtr = &inputClass;
    v.push_back(inputParentClassPtr); // store address of student
    v[i]->addToVector(input);
    inputParentClassPtr = nullptr; // has no real effect
} // inputClass goes out of scope and is destroyed here

When the block ends, the local "stack" variables from that block are destroyed. That means the Graduate is no longer a valid object and the pointer you stored in v is now pointing at something unusable.

To fix it, you really should change your vector to store unique_ptr<Student> but if you can’t do that, you need to allocate the student objects using new like this:

Student* inputParentClassPtr = new Graduate();

That way, even though inputParentClassPtr is still destroyed at the end of the block, it is only a pointer and the Graduate it pointed to is still alive. You’ll then need to delete all the student pointers in the vector at the end, or you’ll have a memory leak.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading