take a look at friend function i am trying to implement
#include <iostream>
#include <string>
using namespace std;
class Customer {
friend void displayInfo(Customer, City);
private:
int custNum;
int zipCode;
};
class City {
friend void displayInfo(Customer, City);
string cityName;
int zipCode;
};
void displayInfo(Customer cus, City city) {
cout << cus.custNum; //compiler error - Inaccessible
}
I understand it is inaccessible. However i have the friend function already defined in the class. So why it is not accessible? Thank you
While you are declaring displayInfo() function as a friend of City and Customer classes, the classes City and Customer given to displayInfo as parameters(i.e. in the declaration) are not defined yet.
If you simply add two lines to the top of your code, as shown here, it does compile.
class City;
class Customer;