-
February 19th, 2009, 02:00 PM
#1
Access Violation with helper functions
I have class A trying to access helper functions in class B.
class A has a friend class B declaration and vice verse.
However when class B tries to use the helper function I get an access violation.
Code:
class A{
friend B;
private:
string a;
public:
string returnString(){ return a; }
};
Can anyone tell me what's wrong with that?
Thanks,
Lang
-
February 19th, 2009, 02:35 PM
#2
Re: Access Violation with helper functions
-
February 19th, 2009, 02:42 PM
#3
Re: Access Violation with helper functions
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.
-
February 19th, 2009, 02:45 PM
#4
Re: Access Violation with helper functions
Try cleaning the solution. If you have MSVS 7 and up, click Build -> Clean Solution.
Also close it and remove the following files: *.aps, *.ncb, *.suo
-
February 19th, 2009, 02:47 PM
#5
Re: Access Violation with helper functions
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()
Thanks,
Lang
-
February 19th, 2009, 02:54 PM
#6
Re: Access Violation with helper functions
What I meant was code that calls the function. I see
if (argCar->getPlateNumber() == parking_spots[i]->getPlateNumber())
Are you sure argCar and parking_spots[i] are both valid pointers?
-
February 19th, 2009, 02:59 PM
#7
Re: Access Violation with helper functions
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.
-
February 19th, 2009, 03:09 PM
#8
Re: Access Violation with helper functions
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.
-
February 19th, 2009, 03:21 PM
#9
Re: Access Violation with helper functions
Thanks for the help guys!
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?
Thanks,
Lang
-
February 19th, 2009, 03:24 PM
#10
Re: Access Violation with helper functions
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.
-
February 19th, 2009, 03:31 PM
#11
Re: Access Violation with helper functions
Yes, I tried that and it worked.
Thanks for the help everone.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|