Friend functions ?

This is a discussion on Friend functions ? within the C++ Programming forums, part of the General Programming Boards category; I had written this function [ total_tax() ] which takes an array of objects of type tax and does some ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Friend functions ?

    I had written this function [ total_tax() ] which takes an array of objects of type tax and does some calculations on the private members of objects of tax.

    Code:
    void total_tax (const tax *Tax)
    {
    	float Total = 0, Tax_Value;
    
    	for (int i=0;i<300;i++)
    	
    		Total += *(Tax + i)->charge ;
    
    	Tax_Value = .4 * Total;
    
    	cout <<"\n The total tax to be paid is "<<Tax_Value;
    }
    The friend function declaration in class tax is as follows

    Code:
    class tax  
    {
    public:
    	tax();
    	tax(float);
    	virtual ~tax();
    	friend void total_tax (const tax*);
    private:
    	float charge;
    The main() is as follows..

    Code:
    void main()
    {
    	int i = 0;
    
    	tax *Array[];
    
    	for (i=0;i<100;i++)
    		Array[i] = new tax (3000);
    
    	for (i=100;i<300;i++)
    		Array[i] = new tax (5000);
    
    	total_tax (Array);
    }
    This however doesn't compile. I get errors like


    syntax error : missing ',' before '*'
    left of '->charge' must point to class/struct/union


    Please help.....my OOP test is on Saturday ....
    First hear, then understand, and then, leaving all distractions, shut your mind to outside influences and devote yourself to developing the truth within you.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    1) left of operator -> needs to be a pointer not an object hence drop the *
    2) in main array has no size!
    3)in total_tax you pass as pointer to const tax yet you modify taxs state hence drop the const!
    4)void main() is undefined behaviour. main() should always return an int.

    fix those first.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    Thankx.......

    I removed the const but I still I don't understand why that was causing problems.

    The pointers were in the array, and I was accessing a data member using the pointer, just accessing, so why could const be causing a problem ????
    First hear, then understand, and then, leaving all distractions, shut your mind to outside influences and devote yourself to developing the truth within you.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i misread it. const was fine. ignore that 1 but rest valid problems. you should not make this function a friend but instead access charge through an accessor function.

    float GetCharge() const { return charge;}

    then change...
    Total += *(Tax + i)->charge
    to
    Total += (Tax + i)->GetCharge()

    now friendship need not be granted to the function.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend Functions
    By rculley1970 in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2007, 04:25 AM
  2. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 08:07 AM
  3. Friend template functions
    By lyx in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 02:11 PM
  4. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:37 PM
  5. Visual C++ and friend functions giving errors
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2001, 06:55 PM

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