Vectors and Pointers

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.

Here's what I have so far:

person.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
#include <vector>

using namespace std;
class person
{
public:
	vector<person*> people;
	person(string,int);
	person* set_person(string);
	string name;
	int age;
	int get_age();
	int update_age();
	string get_name()const;

};
#endif 


person.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>
#include "person.h"

using namespace std;


person::person(string n, int a)
{
	name=n;
	age=a;

	set_person(n);
}

int person::get_age()
{
	return age;
}

string person::get_name() const
{
	return name;
}

int person::update_age()
{
	return age+1;
}

person* person::set_person(string n)
{
	people.push_back(n);
	
}


car.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class car
{
public:
	vector<car*> cars;
	car();
	car(string);
	
};
#endif 


car.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include "car.h"

using namespace std;

car::car()
{
}

car::car(string n)
{


}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include "person.h"
#include "car.h"

using namespace std;

int main()
{
	int number=0;

	person* p=new person("Joe",21);
	
system("pause");
return 0;
}


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.
Last edited on
You are trying to push back a std::string into a vector that wants a person*. How is that even compiling?
It isn't compiling. I am trying to figure out what/how to pass it to get it to work
You have to pass it a person*. Like this or something:

1
2
3
person* someinstance = new person;
people.push_back(someinstance);
// remember to delete 


But in any case, you seem to want to have a vector of person classes the user enters; why, then, does each class have a vector of persons?
OK, I've modified my main to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include "person.h"
#include "car.h"

using namespace std;

int main()
{
	vector<person*> people;

	bool more=true;
	while(more)
	{
	cout<<"Enter name, q to quit: ";
	string name;
	getline(cin,name);
	if(name=="q")
	{
		more=false;
	}
	else
	{
		people.push_back(new person(name));

	}
	}

	for(int i=0; i<people.size(); i++)
	{
		cout<<(*people[i]).get_name();
	}
system("pause");
return 0;
}


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?
Last edited on
Don't make another 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();
}
You could also just push_back people and let the vector do the dynamic allocation for you. (Unless of course you need the pointers for polymorphism)
Topic archived. No new replies allowed.