overloading << problems

This is a discussion on overloading << problems within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to overload << as a friend class. As far as I understand, if class "a" declares ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    overloading << problems

    Hi,

    I am trying to overload << as a friend class. As far as I understand, if class "a" declares "friend class b" then class b should be able to access all private and public members of class a, if this is incorrect then I have more problems than I thought. Otherwise, why doesn't this code work? The compiler error is :
    error C2248: 'emp_name' : cannot access private member declared in class 'employee'

    This is the relevant segment of the code:

    Code:
    class employee
    {
    private:
    	char *emp_name;
    	double rate, current_charge;
    	int current_job_hrs;
    public:
    	employee(double e_rt, char *e_nam="Able", int e_curr_hrs=0) 
    		: rate(e_rt), current_job_hrs(e_curr_hrs)
    	{ 
    		strcpy(emp_name, e_nam);
    	}
    
    	~employee()
    	{
    		cout << "Consultant destructor called for "
    			 << *emp_name
    			 << " by Matt Rossner and Vic Uppal"
    			 << endl
    			 << endl;
    
    		delete emp_name;
    	}
    
    	void set_current_job_hrs(int hrs) { current_job_hrs = hrs; }
    
    	virtual void calc_charge() { current_charge = current_job_hrs * rate; }
    
    	friend ostream& operator <<(ostream& out, const employee& emp);
    
    };
    
    class home_employee : public employee
    {
    private:
    	double office_rate;
    
    public:
    	home_employee(char *he_name, double he_rt, double he_ofc_rt=5);
    	
    	friend ostream& operator <<(ostream& out, const employee& emp)
    	{
    		out << "For home employee named "
    			<< emp.emp_name
    			<< "the charges so far are $"
    			<< emp.current_charge;
    
    		return out;
    	}
    	
    };
    
    home_employee::home_employee(char *he_name, double he_rt, double he_ofc_rt)
    	: employee(he_rt, he_name), office_rate(he_ofc_rt) {}
    
    ostream& operator <<(ostream& out, const home_employee& emp)
    {
    	out << "For employee named "
    		<< emp.emp_name
    		<< "the charges so far are $"
    		<< emp.current_charge;
    
    	return out;
    }
    Thanks for any help out there.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    Looks like you are confusing a few things. Don't confuse the difference between a friend function and a friend object.

    All of the "operator <<()" functions are just that, functions - ie. not methods of any class. The compiler knows what to call by type matching on the functions parameters.

    This function is not declared as a friend of any object
    Code:
    ostream& operator <<(ostream& out, const home_employee& emp)
    If you want this function to access non-public members of home_employee, then the function must be declared a friend in that class.

    If you want to use polymorphism for printing, then add something like this to your base class, then you only need to overload operator<<() once for the base class by simply calling print()
    Code:
    virtual ostream& print(ostream &out) const;
    gg

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Member or friend functions of a derived class do not have a direct access to private data of base class.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    thanks alot, I will make some modifications

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading <<
    By msshapira in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2009, 02:11 PM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. Overloading the << operator.
    By antex in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2005, 01:37 AM
  4. problems overloading outstream for custom ADT
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2005, 09:28 AM
  5. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21