Friend function error: member is inaccessible
Sep 11, 2011 at 4:54am UTC
This is the part of the program that's giving me trouble:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
class Money
{
public :
friend Money operator +(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount);
friend Money operator ==(const Money& amount1, const Money& amount2);
Money(long dollars, short cents);
Money(long dollars);
Money();
double get_value() const ;
friend istream& operator >>(istream& ins, Money& amount);
friend ostream& operator <<(ostream& outs, Money& amount);
private :
long all_cents;
};
//main part of program goes here
ostream& operator <<(ostream& outs, const Money& amount)
{
long positive_cents, dollars, cents;
positive_cents = labs(amount.all_cents);
dollars = positive_cents/100;
cents = positive_cents%100;
if (amount.all_cents < 0)
{
outs << "-" ;
}
outs << '$' << dollars << '.' ;
if (cents < 10)
{
outs << '0' ;
}
outs << cents;
return outs;
}
This program gives me an error described in the title. When I remove the "const" (line 23) it goes away. Does the problem go away after deleting "const" because the compiler is afraid that I may change the value of "amount.all_cents" or is this error the results of some other reason? Thanks.
Sep 11, 2011 at 4:57am UTC
And is there any way that I could keep the "const" part in the program?
Sep 11, 2011 at 5:11am UTC
That's because your prototype in the class (line 15) is taking a non-const Money reference, unlike your definition.
Topic archived. No new replies allowed.