Parking lot Simulator

Hi guys,

I am having an issue with this parking lot simulation
my code is not accepting the police officer class as a friend in the parked car class.

This is the error I am getting

Error 1 error C2065: 'ParkedCar' : undeclared identifier d:\libraries\my documents\visual studio 2013\projects\project 6\project 6\policeofficer.h 32

Error 3 error C2245: non-existent member function 'PoliceOfficer::checkTime' specified as friend (member function signature does not match any overload) d:\libraries\my documents\visual studio 2013\projects\project 6\project 6\parkedcar.h 40

ParkedCar.h
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
#ifndef PARKEDCAR_H
#define PARKEDCAR_H

#include <string>
#include <vector>
#include "PoliceOfficer.h"

using namespace std;

class ParkedCar
{
	private:
	string make;
	string model;
	string color;
	string license;
	string pTime;
	static int carCount;


	public:
	ParkedCar ();
	~ParkedCar ();

	// Mutator functions
	void setMake (string);
	void setModel (string);
	void setColor (string);
	void setLicense (string);
	void setPTime (string);
	void addCars ();
	// Accessor functions
	string getLicenseNumber () const;
	string getModel () const;
	string getColor () const;
	string getMake () const;
	string getPTime () const;
	int getCars () const;

	friend void PoliceOfficer::checkTime (vector<ParkedCar> &, vector<ParkedCar> &);
};
#endif 



PoliceOfficer.h
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
#ifndef POLICEOFFICER_H
#define POLICEOFFICER_H

#include <string>
#include <vector>

using namespace std;

class PoliceOfficer
{
	private:
	string clockIn;
	string officerName;
	string badgeNum;
	string meterTime;
	static int policeCount;

	public:
	PoliceOfficer ();
	~PoliceOfficer ();

	// Mutator Functions
	void setTime (string);
	void setName (string);
	void setBadgeNum (string);
	void countPolice ();
	// Accessor functions
	string getClockIn () const;
	string getName () const;
	string getBadgeNum () const;

	void checkTime (vector<ParkedCar> &, vector<ParkedCar> &);
};
#endif 


I am also getting errors with those vectors

Error 6 error C2923: 'std::vector' : 'ParkedCar' is not a valid template type argument for parameter '_Ty' d:\libraries\my documents\visual studio 2013\projects\project 6\project 6\policeofficer.h 32

Error 11 error C2664: 'void PoliceOfficer::checkTime(std::vector &,std::vector &)' : cannot convert argument 1 from 'std::vector<ParkedCar,std::allocator<_Ty>>' to 'std::vector &' d:\libraries\my documents\visual studio 2013\projects\project 6\project 6\jamescombscs1337project6.cpp 110

I am lost on how to format my logic correctly.
I am trying to keep the code minimal so let me know if you want to see the .cpp's

Even if I include the ParkedCar header in PoliceOfficer.h it does not work. It is saying that ParkedCar is an undeclared identifier in the vectors as well.

As well friend void PoliceOfficer::checkTime (vector<ParkedCar> &, vector<ParkedCar> &);
Last edited on
You've got a circular dependency between PoliceOfficer and ParkedCar.
http://stackoverflow.com/questions/4816698/avoiding-circular-dependencies-of-header-files
ParkedCar uses PoliceOfficer and PoliceOfficer uses ParkedCar.

The usual solution is to do a forward declaration of one of the classes, but that's a problem here because you're trying to reference ParkedCar class as the type parameter to a vector in both headers. You can't use an incomplete class as a type parameter to a vector.

I'm really wondering what the purpose of checkTime is and why it has to be a friend function or a member of PoliceOfficer.

Can you show your code for checkTime() ?
Last edited on
basically check time has nothing right now.

The point of check time was to have the police officer check the time that the parked car left the parking lot. It was going to compare the times and differences and determine if a parking ticket needs to be made. Which will in turn have to have something to do with the ParkingTicket class. All of this is in development and I have changed the entire structure of the code too many times to try to tell you what has been done before.

Basically, all of this has to be read from a file with no human input and convert strings to integers to determine these things. Extracting the time is a whole different problem since there is a colon between then hours and minutes.
I have another question now

How would I display this vector of objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static const vector<ParkedCar> cars;
...

vector<ParkedCar>cars (0);
....

ParkedCar *pc = new ParkedCar ();
...
cars.push_back(*pc);'
...
for (int i = 0; i < 4; i++)
	{
		cout << cars [i].getMake ();
	} 


this does not display anything after I have used the member functions of the dynamically allocated ParkedCars and stored the objects in the static vector.
A couple of comments on that snippet of code.

line 7: There is no need to dynamically allocate pc. Since push_back takes a value parameter, a locally declared pc is just fine.
1
2
3
  ParkedCar pc; 
...
  cars.push_back(pc);


Line 11: Your loop looks fine except for the termination condition. You should be using the size of the vector, not a hard coded value.
 
  for (int i = 0; i < cars.size(); i++)

yes i was just giving an example of a situation.

This is what my main function looks like right now

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstring>
#include <vector>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "ParkingTicket.h"
#include "PoliceOfficer.h"

using namespace std;

int main ()
{
	fstream inFile ("parking.dat", ios::in);
	ParkedCar *pc;
	ParkingMeter *pm;
	PoliceOfficer *officer;
	vector<ParkedCar> cars (0);
	vector<ParkingMeter> meter (0);

	int spot;
	string holdData;
	string holdSpot;

	if (inFile.fail ())
	{
		cout << "Error, could not open parking.txt";
	}

	// started to try to work with the things you were showing me here
	while (inFile.good ())
	{
		inFile >> holdData;

		if (holdData == "enter")
		{
			pc = new ParkedCar ();
			pc->addCars ();

			pm = new ParkingMeter;
			pm->addMeters ();

			inFile >> holdSpot;
			spot = stoi (holdSpot);
			cout << " This is spot " << spot << endl;
			pc->setInSpot (holdSpot);

			inFile >> holdData;
			pc->setPTime (holdData);
			inFile >> holdData;
			pc->setMake (holdData);
			inFile >> holdData;
			pc->setModel (holdData);
			inFile >> holdData;
			pc->setColor (holdData);
			inFile >> holdData;
			pc->setLicense (holdData);
			inFile >> holdData;
			pm->setTimePaid (holdData);

			cars.insert (cars.begin (), spot, *pc);			// I am not worried about performance for this project, I am just worried about making it easier on myself
			// It is due in 4 days.
			meter.insert (meter.begin (), spot, *pm);

			cout << "This is the vector spot " << cars [spot - 1].getInSpot () << endl << endl;			// test code. I now know meter was stored correctly as well.

			// just to see what is going on.
			cout << pc->getPTime () << " " << pc->getMake () << " " << pc->getModel () << " " << pc->getColor () << " " << pc->getLicenseNumber () << " " << pm->getTimePaid () << endl;
		}
		else if (holdData == "in")
		{
			// let a new police officer come in
			officer = new PoliceOfficer;
			officer->countPolice ();

			// get his data needed.
			inFile >> holdData;
			officer->setName (holdData);
			inFile >> holdData;
			officer->setBadgeNum (holdData);
			inFile >> holdData;
			officer->setIn (holdData);
		}
		else if (holdData == "out")
		{
			//delete officer;			// whether this is commented out or not, VS 2013 Ultimate will not let me run this code do to a potentially uninitialized pointer.
			// If I show you the file, the way it is structured, there will be a pointer. VS does not know this
		}

		else if (holdData == "exit")
		{
			string eTime;	// get exit time

			inFile >> holdSpot;	// get parking spot
			spot = stoi (holdSpot);
			inFile >> eTime; // get exit time

			cout << " now calling officer->checkTime\n";
			officer->checkTime (pc, eTime); // police officer checks the time

			//delete pm;
		}
	}

	// this was just test code to see what was happening to my member functions and if memory was stored correctly.

	//for (int i = 0; i < 4; i++)
	//{

	//}

	cout << "\n\n";
	inFile.close ();

	system ("pause");
	return 0;
}


Visual Studios will not allow me to run this code due to my pointer logic now but in the file it makes sense, the comiler does not know this. How would I get the compiler to know that it is ok to have these pointers doing these things. I can also provide the file, just ask if need be.

basically ive changed the structure again. The goal is to now get these pointers to make logical sense to the compiler. That way I may pass it to the officer and check the time and generate a ticket if need be, then print it to a file. After that I am done with this project
Last edited on
Visual Studios will not allow me to run this code due to my pointer logic now

I assume you're referring to line 88?

What if an "out" record occurs before an "in" record? What is the value of officer in that case? What if you have two "in" records followed by two "out"records". The officer pointer is not going to be valid for the second "out" record.

VS should only generate a warning for an uninitialized pointer. That should not prevent execution of your program, but still indicates a logic error in your program.

You have a memory leak. memory allocated by new is never released.



Topic archived. No new replies allowed.