I'm trying to overload the operator “<<“ as a friend function but for some reason it cannot access members of the class. Why isn't the friend function able to access isEmpty(), count and call_DB[ ] ? This is the code I wrote:
#include <iostream> #include <string> using namespace std; class callRecord { public: string firstname; string lastname; string cell_number; int relays; int call_length; double net_cost; double tax_rate; double call_tax; double total_cost; }; class callClass { public: bool isEmpty(); friend ostream & operator<<(ostream & out, callClass &Org); private: int count; int size; callRecord *call_DB; }; bool callClass::isEmpty() { if (count == 0) { return 1; } else { return 0; } } ostream & operator<<(ostream & out, callClass &Org) { if (isEmpty() == 1) { cout << "The array is empty" << endl; } else { out.setf(ios::showpoint); out.setf(ios::fixed); out.precision(2); for (int i = 0; i < count; i++) { out << setw(10) << call_DB[i].firstname << " "; out << setw(10) << call_DB[i].lastname << " "; out << call_DB[i].cell_number << "\t"; out << call_DB[i].relays << "\t"; out << call_DB[i].call_length << "\t"; out << call_DB[i].net_cost << "\t"; out << call_DB[i].tax_rate << "\t"; out << call_DB[i].call_tax << "\t"; out << call_DB[i].total_cost << endl; } } } Thanks.
callClass::operator<<) or make it a free operator and callOrg::isEmpty().Org.std::endlunless you need the extra stuff that it does.'\n'ends a line.