Pointer help

I am a C++ student and am needing some assistance with the following assignment to where I am to implement a class Person with two fields name and age and a class Car with three fields


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//The purpose of program is to implement a class Person with two fields name and age and a class
//class car with three fields: model, pointer to the owner and a pointer to the driver.
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Person
{
private:
	string name;
	int age;
public:
	Person(string n, int a);
	string get_name() const;
	int get_age() const;
	void Person::increment_age();  // is inside the class body so the prefix Person:: is not needed.
	void print() const;
};

Person::Person(string n, int a){ 
name = n;
age = a;}
string Person::get_name() const{
return name;}
void Person::increment_age(){
age += 1;}
void Person::print() const{
cout << name << endl;
cout << age << endl;}

class Car
{
private:
	string model;
	Person *owner;
	Person *driver;
public:
	Car::Car(string m); //Same as Person:: prefix
	void Car::set_driver(Person* p) {driver = p;}
	void Car::set_owner(Person* p) {owner = p;}
	void print() const; 
};

Car::Car(string m){ model = m;}

void Car::set_owner(Person* p){*owner = *p;}

void Car::print() const{
	cout << model << endl;
	cout << "Driver: "; driver->print();
    cout << "Owner: "; owner->print();}

int main()
{ 

vector<Person*> people;

const int PERSON_SZ = 6;
char * names[] = {"Linda", "Jeffrey", "Sue", "Chad", "Wendy", "Denny"};
int ages[] = {39, 34, 42, 49, 38, 48};

for (int i = 0; i < PERSON_SZ; i++){
Person *a = new Person(names[i], ages[i]);
people.push_back(a);}




vector<Car*> cars;

const int CAR_SZ = 3;
char * models[] = { "Porsche", "Lambourgini", "Corvette" };

for (int i = 0; i < CAR_SZ; i++)
{
Car *c = new Car(models[i]);
c->set_driver(people[rand()% (people.size())]);
c->set_owner(people[rand()% (people.size())]);
cars.push_back(c);
}
system("PAUSE");

return 0;
}



These are the errors I am getting:

(48): error C2084: function 'void Car::set_owner(Person *)' already has a body (42) : see previous definition of 'set_owner' (80): error C2264: 'Car::set_owner' : error in function definition or declaration; function not called
Last edited on
You shouldn't have Car:: in front of function declaration/definitions occurring inside the class.
In lines 42 and 48 you've defined the set_owner() function twice instead of declaring once and defining once.
Last edited on
I fixed that and the other errors I got, now I get no print out.
I found out what I needed to do. Thanks again for your help Zhuge and rcast.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Implement a class Person with two fields name and age, and a class Car with three fields:
•	The model
•	A pointer to the owner (a Person*)
•	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<Person*> and a vector<Car*>. 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.

*/

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//Person Class
class Person
{
private:
	string name;
	int age;
public:
	Person(string n, int a);
	string getName()const;
	int getAge()const;
	void Person::incrementAge();
	void print()const;
};

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

string Person::getName() const{
	return name;
}

void Person::incrementAge(){
	age += 1;
}

void Person::print() const{
	cout << name << "\t" << age << endl;
}
//end of Person Class

//Car Class
class Car
{
private:
	string model;
	Person *owner;
	Person *driver;
public:
	Car::Car(string m);
	void Car::set_driver(Person* p);
	void Car::set_owner(Person* p);
	void print()const;
};


Car::Car(string m){
	model = m;
}

void Car::set_driver(Person* p){
	driver = p;
}

void Car::set_owner(Person* p){
	owner = p;
}


void Car::print() const{
	cout << endl;
	cout << model << endl;
	cout << "Driver: "; driver->print();
	cout << "Owner: "; owner->print();
}
//End of Car Class

//Entry point to program
int main()
{

	vector<Person*> people;

	const int PERSON_SIZE = 4;
	char * names[] = {"Joan", "Jermaine", "Josephine", "Jeremy"};
	int ages[] = { 43, 45, 42, 49 };

	for (int i = 0; i < PERSON_SIZE; i++){
		Person *a = new Person(names[i], ages[i]);		
		people.push_back(a);
	}

	for( int i = 0; i < people.size(); i++ ) {
		(people[i])->incrementAge();
	}


	vector<Car*> cars;

	const int CAR_SIZE = 3;
	char * models[] = { "Cayenne", "Camaro", "Corvette" };

	for (int i = 0; i < CAR_SIZE; i++)
	{
		Car *c = new Car(models[i]);
		c->set_driver(people[rand()% (people.size())]);
		c->set_owner(people[rand()% (people.size())]);		
		cars.push_back(c);
	}

	for( int i = 0; i < cars.size(); i++ ) {
		(cars[i])->print();
	}

	system("PAUSE");
	return 0;
}
You actually don't tell your program to print anything i.e. you don't call the print method for any of the objects you created

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
for (int i = 0; i < CAR_SZ; i++)
{
Car *c = new Car(models[i]);
c->set_driver(people[rand()% (people.size())]);
c->set_owner(people[rand()% (people.size())]);
cars.push_back(c);
}

for (int i = 0; i < CAR_SZ; i++) 
{
(*cars[i]).print();  ////
}

system("PAUSE");

return 0;
}
Last edited on
Thanks too vin, much appreciated
Topic archived. No new replies allowed.