C++ Vector not displaying contained object's data [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

C++ Vector not displaying contained object's data [duplicate]



This question already has an answer here:



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;



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




1 Answer
1



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


userlist


adduser()


delete()

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Mass disable jenkins jobs