0

I have a vector that stores multiple class objects for later access. This way my program can create new objects during runtime. This is done like so:

vector<Person> peopleVector;
peopleVector.push_back(Person(name, age));
for (int i = 0; i < peopleVector.size(); i++) {
    cout << peopleVector[i].name << endl;
}

This function should print out each objects "name" every time the code runs (it's a function that runs multiple times). However, when I run this, somehow the vector does not increase in size. If you add cout << peopleVector.size(); to that code, you will find that each time it runs, it gets one (obviously assuming you also have the class code which I have below).

I'm curious why I can't create multiple objects in the class.

Class.h

#pragma once
#include <iostream>
using namespace std;

class Person {
public:
    Person(string personName, int personAge);
    string name;
    int age;
};



Person::Person(string personName, int personAge) {
    name = personName;
    age = personAge;
}

Main.cpp

#include "Class.h"
#include <random>

int main() {
    // Necessary for random numbers
    srand(time(0));

    string name = names[rand() % 82]; // Array with a lot of names
    int age = 4 + (rand() % 95);
}

// Create a new person
void newPerson(string name, int age) {
    vector<Person> peopleVector;
    peopleVector.push_back(Person(name, age));
    for (int i = 0; i < peopleVector.size(); i++) {
        cout << peopleVector[i].name << endl;
    }
}

Just FYI those #includes might be a little bit off because I took that code out of a large section that had like 15 includes.

1
  • You never call newPerson in your example so what did you expect to happen? Plus you keep making a new std::vector<Person> each time you call newPerson so that vector will never be greater than size 1 Commented Mar 15, 2019 at 22:01

2 Answers 2

2

You are creating an empty vector each time you call your newPerson() function, and then you add a single person to it.

You then display the contents of that vector. What else can it contain, other than the single person that you added?

Sign up to request clarification or add additional context in comments.

Comments

0

Problem

Every time a function runs, all local variables inside the function are re-created in their default state. That means that every time you call newPerson, it just recreates peopleVector.

Solution

There are two solutions:

  • Have newPerson take a reference to a vector, and add it on to that
  • make peopleVector static, so that it isn't re-initialized every time

First solution:

// Create a new person; add it to peopleVector
// The function takes a reference to the vector you want to add it to
void newPerson(string name, int age, vector<Person>& peopleVector) {
    peopleVector.push_back(Person(name, age));
    for (int i = 0; i < peopleVector.size(); i++) {
        cout << peopleVector[i].name << endl;
    }
}

Second solution: mark peopleVector as static

// create a new person; add it to peopleVector
void newPerson(string name, int age) {
    // Marking peopleVector as static prevents it from being re-initialized
    static vector<Person> peopleVector; 
    peopleVector.push_back(Person(name, age));
    for (int i = 0; i < peopleVector.size(); i++) {
        cout << peopleVector[i].name << endl;
    }
}

Comments

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.