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 is what I have:
cars.h:
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
#ifndef CARS_H
#define CARS_H
#include<iostream>
#include<vector>
#include<string>

using namespace std;
 
class Person
{
    private:
	string name;
	int age;
    public:
	Person():name(" "), age(0){}
	Person(string n, int a): name(n),age(a){}
	string get_name();
	int get_age();
	int set_age();
	void print();
};

class Car 
{
    private:
	string model;
	Person*owner;
	Person*driver;
	vector<Person>people;
	vector<Car>cars;
    public:
	Car(): model (), owner(), driver(){}
	Car(string s, Person*o, Person*d): model(s), owner(o), driver(d){}
	void print();
};

#endif 


drivers.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
38
39
40
41
42
43
44
45
#include<iostream>
#include<vector>
#include<string>
#include "cars.h"
using namespace std;
 


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

string Person::get_name()
{
    return name;
}

int Person::set_age()
{
    for (int i=0; i<people.size(); i++){
	age = people[i].get_age();
	age = age+1;
    }
}

void Person::print()
{
    cout << people.get_name() << people.get_age();
}



void Car::print()
{
    cout << model;
    if (owner)
	cout << "Owner: " << (*owner).get_name() << (*owner).get_age();
    else
	cout << "Owner does not exist!";
    if (driver)
	cout << "Driver: " << (*driver).get_name() << (*driver).get_age();
    else
	cout << "Driver does not exist!";
}


and main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<vector>
#include<string>
#include "cars.h"
using namespace std;
 

Person*find(string n, vector<Person>&people)
{
    for(int i=0; i<people.size(); i++)
    {
	if ( n == people[i].get_name())
	    return &(people[i]);
        else 
	    return 0; 
    }
};

int main()
{
    Person *p;
    Person*pp;
    string response;
    string in_name;
    int in_age;
    string in_owner;
    string in_driver;
    string model;
    cout << "Please enter a set of drivers and their ages. Terminate with '-1 -1'.";
    cin >> in_name >> in_age;
    while (in_name != "-1"){					//in driver and age
	people.push_back(Person(in_name, in_age));
	cin >> in_name >> in_age;
    }

    cout << "Now Enter the Car's model, its owner, and its driver. Terminate with 'ctr+d'.";
    cin >> model >> in_owner >> in_driver;
    while (!cin.eof()){
	p=find(in_owner, people);
	pp= find(in_driver, people);			//in model, owner, driver
	cars.push_back(Car(model, p, pp));
	cin >> model >> in_owner >> in_driver;
    }

    cout << "Would you like to increment the drivers' ages? (y/n)";
    cin >> response;
    if (response == "y")
	people.set_age();
    cars.print();
    people.print();

};


Now, my problem is that I am getting an error that reads "‘people’ was not declared in this scope" everywhere i try to work with on my vector, ( driver.cpp line 29 for example). Same thing happens on "cars" as in main.cpp line 49.

Is the problem coming from how I declared my vectors?
1
2
3
4
5
6
7
    private:
	string model;
	Person*owner;
	Person*driver;
	vector<Person>people;
	vector<Car>cars;
    public:

Or from somewhere else?
It looks you are trying to reference Car from Person. People is contained in Car and it is private so it can't be accessed outside of it's implementation.

You are trying to access it from within Person. You can't really do that.

Instead maybe you could create a method in Person that will set a Person's age and then inside the Car class you could set each person's age since you have actually declared objects of Person.

The problem you have here is that inside Person class, you have not declared any Car objects so the compiler doesn't know what you are referring to.


EDIT - Quick nitpick here. Classes are private by default so you don't even need the private keyword. The more conventional way is actually to declare your public members first followed by the private ones.
Last edited on
Oh, I see. Thanks, that helped quite a bit!
Topic archived. No new replies allowed.