cybersoul
cybersoul

Reputation: 19

Friend function not able to access member of class C++

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.

Upvotes: 1

Views: 499

Answers (4)

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36401

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.

Upvotes: 0

Slava
Slava

Reputation: 44258

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.

Upvotes: 0

A.S.H
A.S.H

Reputation: 29332

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.

Upvotes: 2

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

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();.

Upvotes: 2

Related Questions