CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Access Violation with helper functions

    more code please.

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jan 2009
    Posts
    16

    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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?

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  9. #9
    Join Date
    Jan 2009
    Posts
    16

    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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  11. #11
    Join Date
    Jan 2009
    Posts
    16

    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
  •  





Click Here to Expand Forum to Full Width

Featured