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.
#include<iostream>
#include<vector>
#include<string>
#include "cars.h"
usingnamespace 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]);
elsereturn 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?
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.