I am working on something where I have a class "person" with two fields name and age. A class "car" with 3 fields, the model, a pointer to the owner (person *), a pointer to the driver(also a person*). The user will specify people and cars. Store them in vector<person*> and vector<car*>. I need to traverse the vector of person objects and incremement ages by one year. The travers the vector of cars and print out the car model, owners name and age, and drivers name and age.
As you can see it returns an error when I try to set_person(). As of now, I am taking it step by step so the first step I'm trying is to add people to the people vector. Am I on the right track? I have no experience really with pointers so I am kinda going off examples in the book. Thanks for your help so much.
This compiles and I am able to add new objects to the vector. If I need to keep track of name and age of the people, should I make another vector that stores the age? Or is there a way to have 2 fields in one record in the people vector?
You can change your 'person' class to have members for age and name. You can also change the constructor to accept an age:
1 2 3 4 5 6
// of course for this code to work, you'd need to change the 'person' class
people.push_back( new person(name,age) );
//...
cout << (*people[i]).get_age();
Also -- just so you know, this program has a memory leak because you're allocating things with new, but never deleting them.
If you want to clean this up:
1 2 3 4 5 6 7
// do this before you exit main()
// this will empty the vector and delete each person allocated with new
while(!people.empty())
{
delete people.back();
people.pop_back();
}