I keep getting an error on this program. My VS 2013 keeps giving me a IntelliSou
ID: 3555720 • Letter: I
Question
I keep getting an error on this program. My VS 2013 keeps giving me a IntelliSource: declaration is incompatible with "Void Car::set_Driver(*p) delcared at line 35.
The problem to solve is:
"Implement a class Person with two fields name and age, and a class Car with 3 fields: 1) the Model; 2) A pointer to the owner (a Person*) and 3) A pointer to the driver (also a Person*).
Write a program that prompts the user to specify people and cars. Store them in a vector and a vector . Traverse the vector of Person objects and increment their ages by one year. Traverse the vector of cars and print out the car model, owner's name and age, and driver's name and age.
Here is what I have so far:
#include
#include
#include
using namespace std;
class Car
{
private:
string model;
Person* owner;
Person* driver;
public:
Car::Car(string m);
string getDriver(string driver);
void set_Driver(Person*p);
void set_Owner(Person*p);
void print() const;
};
Car::Car(string m)
{
model = m;
}
string Car::getDriver(string driver)
{
cout << "Please enter the driver's name";
cin >> driver;
return driver;
}
void Car::set_Driver(Person*p)
{
*driver= *p;
}
void Car::set_Owner(Person*p)
{
*owner=*p;
}
void Car::print() const
{
cout< cout< cout << &driver << endl;
cout << &owner << endl;
}
class Person
{
private:
string name;
int age;
public:
Person(string n, int a);
int getAge() const;
string getName() const;
void Person::increaseAge();
void print() const;
};
Person::Person(string n, int a)
{
name = n;
age = a;
}
string Person::getName() const
{
return name;
}
void Person::increaseAge()
{
age += 1;
}
void Person::print() const
{
cout << name << endl;
cout << age << endl;
}
int main()
{
vectorpeople;
const int PERSON_SZ = 4;
char* Name[] = { "Bob", "John", "Sue", "Sally" };
int Age[] = { 33, 34, 40, 41 };
for (int i = 0; i < PERSON_SZ; i++)
{
Person*a = new Person(Name[i], Age[i]);
people.push_back(a);
}
vectorcars;
const int CAR_SZ = 3;
char* model[] = { "Festiva", "Honda", "Toyota" };
for (int i = 0; i < CAR_SZ; i++)
{
Car*c = new Car(model[i]);
c->set_Driver(people[rand() % (people.size())]);
c->set_Owner(people[rand() % (people.size())]);
cars.push_back(c);
}
system("PAUSE");
return 0;
}
Explanation / Answer
Indented code can be found at: https://dl.dropboxusercontent.com/u/42883368/chegg/ans.cpp
Their were many errors and I removed them.
/******************************* ans.cpp *******************************************/
#include <stdio.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person(string n, int a);
int getAge() const;
string getName() const;
void increaseAge();
void print() const;
};
Person::Person(string n, int a)
{
name = n;
age = a;
//cout << n << endl;////////////
}
string Person::getName() const
{
return name;
}
void Person::increaseAge()
{
age += 1;
}
void Person::print() const
{
cout << name << endl;
cout << age << endl;
}
class Car
{
private:
string model;
Person* owner;
Person* driver;
public:
Car(string m);
string getDriver(string driver);
void set_Driver(Person*p);
void set_Owner(Person*p);
void print() const;
};
Car::Car(string m)
{
model = m;
}
string Car::getDriver(string driver)
{
cout << "Please enter the driver's name";
cin >> driver;
return driver;
}
void Car::set_Driver(Person*p)
{
driver= p;
}
void Car::set_Owner(Person*p)
{
owner=p;
}
void Car::print() const
{
cout << &driver << endl;
cout << &owner << endl;
}
int main()
{
vector<Person*> people;
const int PERSON_SZ = 4;
char* Name[] = { "Bob", "John", "Sue", "Sally" };
int Age[] = { 33, 34, 40, 41 };
for (int i = 0; i < PERSON_SZ; i++)
{
Person*a = new Person(Name[i], Age[i]);
people.push_back(a);
}
vector<Car*> cars;
const int CAR_SZ = 3;
char* model[] = { "Festiva", "Honda", "Toyota" };
for (int i = 0; i < CAR_SZ; i++)
{
Car*c = new Car(model[i]);
c->set_Driver(people[rand() % (people.size())]);
c->set_Owner(people[rand() % (people.size())]);
cars.push_back(c);
}
system("PAUSE");
return 0;
}