Bad design. If A needs B's privates, and B needs A's privates, then maybe you need an C class with those parts, and make A and B derived from it. However, that depends on what exactly one needs from the other. However, you should avoid using friends at all.
Ok, thanks for the quick reply. GCDEF asked for more code so this is class Car.
Code:
//We fordward reference the parkingLot class because Class Car has yet to be defined
//but our parkingLot class requires it.
class parkingLot;
using namespace std;
class Car{
friend parkingLot;
private:
string plateNumber;
public:
Car (string argString){
this->plateNumber = argString;
}
string getPlateNumber(){ return plateNumber; }
};
This is class parkingLot;
Code:
class parkingLot{
friend Car;
private:
int lot_number; // the identification number of the parking lot
int capacity; // the maximum number of car it can accommodate
int car_count; // the number of cars currently in the lot
double hourly_rate; // the hourly rate
double max_charge; // the maximum charge for each car
double revenue; // the daily revenue of this rate
Car ** parking_spots; // a dynamic array of pointers to Car,
// representing parking spots in the lot
public:
parkingLot(int argNumber, int argCapacity, double hourlyRate, double maxCharge){
lot_number = argNumber;
capacity = argCapacity;
hourly_rate = hourlyRate;
max_charge = maxCharge;
revenue = 0;
car_count = 0;
parking_spots = new Car *[capacity];
}
int enter(Car * argCar, int hour, int min){
if (car_count == capacity) return 1; //parking lot full
for (int i = 0; i < capacity; ++i){
if (parking_spots[i] != NULL){ // we have to make sure its not null to compare license plates
if (argCar->getPlateNumber() == parking_spots[i]->getPlateNumber()){
return 2; //2 = car already in parking lot
}
}
}
for (int i = 0; i < capacity; ++i){
if (parking_spots[i] == NULL){
parking_spots[i] = argCar; //found an empty spot, break, there's no use to keep looking
break;
}
}
argCar->park(this, hour, min);
cout << argCar->toString() << " enters Lot " << lot_number << " at " << hour << ":" << min;
++car_count;
return 0; // no error
}
};
I just need class parkingLot to have the ability to call Car::getPlateNumber()
I just need class parkingLot to have the ability to call Car::getPlateNumber()
And why do they have to be friends for that? :confusion: That is a public methods. As long as you have an object or valid pointer to object of type Car, you can call it. No need for friends. As I already said, don't use friends. Friends are like guns. For the simple fact that you have one, but you don't have to use it.
cilu makes a good point. As long as getPlateNumber is public, which it is, you don't need a friend class. That's not the cause of your crash however. Check your pointers when you try to call the function.
parking_spots[i] was not NULL but it was an invalid pointer. Is there anyway to detect that? Or should I just set every value in my parking_spots array to NULL?
After this line
parking_spots = new Car *[capacity];
you'd need to initialize it to NULLs. You're declaring a bunch of pointers, but not initializing them.
Bookmarks