Need Help to fix the errors.

hi there,

I am new to C++,here is my first homework for this semester. I really don't know how to fix these errors. please help!!

thanks


here are the errors I got after I build the solution:

1>error C3861: 'findPerson': identifier not found
1>error C3861: 'findPerson': identifier not found
1>error C2664: 'Car::Car(std::string,Person *,Person *)' : cannot convert parameter 2 from 'Car *' to 'Person *'
1>Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>warning C4018: '<' : signed/unsigned mismatch
1>warning C4018: '<' : signed/unsigned mismatch
1>error C2447: '{' : missing function header (old-style formal list?)

and my code:
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
124
125
126
127
128
129
130
131
132
133
#include <iostream>
using namespace 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;
}



Last edited on
int driver_index = findPerson(carList,driver);

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.



thanks for replying me!!

So should I put the function findPerson above the int main ()??

carList.push_back(new Car(model,carList[owner_index],carList[driver_index]));

can I use delete new? and just write carList.push_back(Car(model,carList[owner_index],carList[driver_index]));

thanks again!! it really helps a lot!!
you should edit the original post with code tags.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//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!) 

Last edited on
what is code tags?
These are code tags: [code][/code]
Topic archived. No new replies allowed.