Declared a function as a friend of a class but the member is still inaccessible

I have a class called ParkingLot with a function called removeCarsThatWantToLeave and I declared this function a friend of my Space class so that removeCarsThatWantToLeave can have access to the occupyingCar pointer that is a member of Space


This is the function in ParkingLot.cpp that I'm working on but my compiler shows a red squiggly line under .occupyingCar and says it is inaccessible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
Description:  iterates through the array of parking spaces
and destroys the cars that have been parked as long as the amount
of time specified in their minutesToPark member
Return: void
*/
void ParkingLot::removeCarsThatWantToLeave(int curTime)
{
	int timeParked; //amount of time car in a space has been parked
					//this is a temp variable that will be used 
					//for the time parked of all cars in the lot
	

	for (int i = 0; i < numParkingSpaces; i++)
	{
		timeParked = parkingSpaceArray[i].occupyingCar->
		//if (parkingSpaceArray[i].occupyingCar->
	{

}




ParkingLot.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include <iostream>
#include <list>


//forward declarations
class Car;
class Space;


using namespace std;

class ParkingLot
{
private:

	//making this a singleton because there will be only one parking lot and 
	//it will make global access to the parking lot easier. It won't need to be
	//passed between functions a lot this way.
	static ParkingLot* parkingLotInstance;

	//will use array because I won't need to change 
	//the number of parking spaces there are
	Space* parkingSpaceArray; 

	bool isFull;

	int numParkingSpaces;

	int numFilledParkingSpaces;

public:
	ParkingLot(void);

	static ParkingLot* getInstanceOfParkingLot();

	//returns index of the first empty parking space in the array
	//called by Car::parkCar
	int getIndexOfFirstEmptyParkingSpace();

	void addCarToParkingSpaceArray(int indexOfEmptyParkingSpace, Car* carToAdd);

	void removeCarsThatWantToLeave(int curTime);

	
	//setters
	void setNumParkingSpaces(int n);

	void setNumFilledParkingSpaces(int n);


	//getters
	bool getIsFull();

	int getNumParkingSpaces();

	int getNumFilledParkingSpaces();

	~ParkingLot(void);
};



#include "Car.h"
#include "Space.h" 





Space.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
43
44
45
#pragma once

class Car;

using namespace std;
class Space
{

private:

	//need this because I shouldn't just check whether occupyingCar points to null
	//since that would involve a public method returning a pointer to a private member
	bool isOccupied; 
	int minutesPurchased;

	Car* occupyingCar; //pointer to car that occupies this space


public:
	Space(void);

	//friend
	friend void ParkingLot::removeCarsThatWantToLeave(int curTime);


	//setters
	void setIsOccupied(bool i);

	void setMinutesPurchased(int m);

	void setOccupyingCar(Car o);


	//getters
	bool getIsOccupied();

	int getMinutesPurchased();

	

	~Space(void);
};


#include "Car.h" 




Space.cpp
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
#include "Space.h"


Space::Space(void)
{
	isOccupied = false;
	occupyingCar = 0;
}

void Space::setIsOccupied(bool i)
{	
	isOccupied = i;
}	

void Space::setMinutesPurchased(int m)
{	
	minutesPurchased = m;
}	

void Space::setOccupyingCar(Car o)
{
	occupyingCar = &o;
}



bool Space::getIsOccupied()
{
	return isOccupied;
}

int Space::getMinutesPurchased()
{
	return minutesPurchased;
}


Space::~Space(void)
{
}
Last edited on
Topic archived. No new replies allowed.