#include <iostream>
usingnamespace std;
#include <vector>
using std::vector;
#include<string>
class Person
{
public:
Person(string n, int a)
{
name = n;
age = a;
}
void incrementAge()
{
age++;
}
string getName()
{
return name;
}
int getAge()
{
return age;
}
private:
string name;
int age;
};
class Car
{
public:
Car(string m,Person *o,Person *d)
{
model = m;
owner = o;
driver = d;
}
void print()
{
cout<<"car model is "<<model<<endl;
cout<<"owner's name is "<<owner->getName()<<endl;
cout<<"owner's age is "<<owner->getAge()<<endl;
cout<<"driver's name is "<<driver->getName()<<endl;
cout<<"driver's age is "<<driver->getAge()<<endl;
}
int findPerson(vector<Person*> list, string searchName);
private:
string model;
Person *owner;
Person *driver;
};
int main()
{
int age;
string name, model, driver, owner;
vector<Person*> personList;
vector<Car*> carList;
cout<<"enter person age";
cin>> age;
while (age != -1)
{
cout<<"enter person name";
cin>> name;
personList.push_back(new Person(name,age));
cout<<"enter person age";
cin>> age;
}
cout<<"enter car model";
cin>> model;
while (model != "none")
{
cout<<"enter owner name";
cin>> owner;
cout<<"enter driver name";
cin>> driver;
int driver_index = findPerson(carList,driver);
int owner_index = findPerson(carList,owner);
carList.push_back(new Car(model,carList[owner_index],carList[driver_index]));
cout<<"enter car model";
cin>> model;
}
for (int i = 0; i < personList.size(); ++i)
{
personList[i]->incrementAge();
}
for (int i = 0; i < carList.size(); ++i)
{
carList[i]->print();
}
}
int findPerson(vector<Person*> list, string searchName);// returns location
{
for (int = 0; i < list.size(); ++i)
{
if (searchName == list[i]->getName())
{
return i; //strings are equal; return, index of person in vector
}
}
return -1;
}
This line of code is trying to use the function findPerson. At this point in your code, the compiler does not know what that function is. I see that you have pre-declared that function.... but buried inside your car class. The predeclaration of this function has no business being buried inside that class - it's not a class function so why put that in there?
carList.push_back(new Car(model,carList[owner_index],carList[driver_index]));
You seem to be trying to create a new object of type Car using a constructor that does not exist. Your Car constructor Car(string m,Person *o,Person *d) takes a string, a pointer to a Person, and a pointer to a Person.
This: new Car(model,carList[owner_index],carList[driver_index]) appears to be an attempt to pass in a string, a pointer to a Car, and a pointer to a Person. Also, why are you using new? You have no need for it here.
At the end, where you try to define the function findPerson, you've put a semi-colon at the end of int findPerson(vector<Person*> list, string searchName);// returns location
See it there? I've underlined it. That should not be there.
//assume some class T
T( ... ) //creates a T object on the stack and returns it
new T( ... ) //creates a T object on the heap, and returns a pointer to it
//Examples:
f( T( ... ) ); //pass a newly created T object to the function "f", then throw that object away (after f returns)
T t( ... ); //create a new T object and store it as "t"
f(t); //pass that object to the function "f"
f( new T( ... ) ); //pass a pointer to a newly created T object, then throw the pointer away (could cause a memory leak!)
T *p = new T( ... ); //create a new T object, and store a pointer to it in "p"
f(p); //pass that pointer to the function "f"
f(*p); //pass the T object to the function "f"
delete p; //delete the object when we are done with it (important!)