0

I'm new to C++. I've searched around a bit but cannot find something that helps. Very simple program, just trying to store 'Person' objects in a Vector and access those objects. My Vector, as of now, holds one object of type 'Person'. 'Person' objects have a 'name' field. I'm simply trying to display that 'name' field. But when the program runs, nothing prints to the console.

I'm still getting comfortable with Vectors and pointers, so my vector setup may be the cause. My 'getName()' function is setup correctly. Can someone please let me know if the way I'm declaring and inserting objects into my vector, as well as the vector declaration/iteration, is correct?

#include <iostream>
#include <string> 
#include <ctime>
#include <vector>
#include "Person.h" 
#include "Record.h" 


//addition of a user to list 
void addUser(int id, int age, std::string name, std::vector<Person*> userList) 
{
    Person *newPerson = new Person(id, age, name); 
    userList.push_back(newPerson);


}

//deletion of a user of the list by ID 
void deleteUserByID(int id, std::vector<Person*> userList) 
{

    for(int i = 0; i < userList.size(); i++) {

        if (userList.at(i)->getID() == id)
        delete userList.at(i); 
    }
}

//Print all user names in the list 
void printUserList(std::vector<Person*> userList) 
{

   for(auto it = std::begin(v); it != std::end(v); it++)
   {

       std::string name = it->getName(); 


   }


}


int main () 
{
    //create vector 
   static std::vector<Person*> userList; 

   //add first user
   addUser(626968231, 21, "Ryan", userList); 


   //print all users (just one so far) 
   printUserList(userList); 

    return 0; 
}
  • No option to answer question for some reason, so a comment must suffice: In your printUserList function, your userList arg is never referenced and v is undefined. – tuk Jul 29 '18 at 06:25

1 Answers1

0

The fourth argument userlist of function adduser()is of call by value change it to call by reference. And do same for the delete() function.

Drizzy
  • 138
  • 8