1

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.

3
  • Either make the operator part of your class (callClass::operator<<) or make it a free operator and call Org::isEmpty(). Commented Apr 12, 2017 at 15:35
  • They need to be invoked on Org. Commented Apr 12, 2017 at 15:37
  • This isn't the problem, but don't use std::endl unless you need the extra stuff that it does. '\n' ends a line. Commented Apr 12, 2017 at 15:37

4 Answers 4

2

Your operator<< function is a global function and not a member of callClass. To access those fields, you need to use Org.call_DB, Org.count and Org.isEmpty();.

Sign up to request clarification or add additional context in comments.

Comments

2

Because you did not "qualify" those members to an instance. You need to write

ostream & operator<<(ostream & out, callClass &Org) { if (Org.isEmpty() == 1) // ^^^^^ 

similary Org.count and Org.call_DB etc ..

Remember that your operator<< is a global function, not a member function. Otherwise you wouldn't have the need to declare it as friend in the first place.

Comments

0

Why isn't the friend function able to access isEmpty(), count and call_DB[ ] ?

Because you do not understand what "access" means. It does not mean that friend function becomes a method of the class, so you cannot implicitly use this as you trying to do, you should use Org argument:

out << setw(10) << Org.call_DB[i].firstname << " "; 

same for the rest.

Comments

0

Your friend function is not a member function so there is no 'this' available. Your operator code should looks like:

ostream & operator<<(ostream & out, callClass &Org) { for (int i = 0; i < count; i++) { out << Org.call_DB[i] << endl; } } } 

and then you must also provide an operator << for callRecord.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.