Friends Of Classes

Hi all,

I am doing some review on class basics and according to the book that I am working out of, it states that friend functions are not members of the class but friend functions can access the private members of the class directly. The program I am trying to write comes directly from the book I am using. The IDE I have is Microsoft Visual C++ 2010 Express. When I type the program in from the book, word for word, it gives the error that on lines 102 and 106, allCents can not be accessed. Can someone explain why it does not impose any problems for the overloaded >> function, but I can not access allCents in the overloaded >> function? Any and all help is greatly appreciated.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 #include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

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 bool operator ==(const Money &amount1, const Money &amount2);
		Money(long dollars, int cents);
		Money(long dollars);
		Money();
		double getValue() const;
		friend istream& operator >>(istream& ins, Money &amount);
		friend ostream& operator <<(ostream& outs, Money &amount);

	private:
		long allCents;
};

int digitToInt(char c);

int main()
{
	char ch;
	Money amount;
	ifstream inStream;
	ofstream outStream;

	inStream.open("infile.txt");
	if(inStream.fail())
	{
		cout << "Input file opening failed.\n";
		cin.get(ch);
		exit(1);
	}

	outStream.open("outfile.txt");
	if(outStream.fail())
	{
		cout << "Output file opening failed.\n";
		cin.get(ch);
		exit(1);
	}

	inStream >> amount;
	outStream << amount << " copied from the file infile.txt.\n";
	cout << amount << " copied from the file infile.txt.\n";

	inStream.close();
	outStream.close();

	cin.get(ch);
	return 0;
}

istream& operator >>(istream &ins, Money &amount)
{
	char oneChar, decimalPoint, digit1, digit2;
	long dollars;
	int cents;
	bool negative;

	ins >> oneChar;
	if(oneChar == '-')
	{
		negative = true;
		ins >> oneChar;
	}
	else
		negative = false;

	ins >> dollars >> decimalPoint >> digit1 >> digit2;

	if(oneChar != '$' || decimalPoint != '.' || !isdigit(digit1) || !isdigit(digit2))
	{
		cout << "Error illegal form for money input\n";
		cin.get(oneChar);
		exit(1);
	}

	cents = digitToInt(digit1) * 10 + digitToInt(digit2);
	amount.allCents = dollars * 100 + cents;

	if(negative)
		amount.allCents = -amount.allCents;

	return ins;
}
int digitToInt(char c)
{
	return ( static_cast<int>(c) - static_cast<int>('0') );
}
ostream& operator <<(ostream &outs, const Money &amount)
{
	long positiveCents, dollars, cents;
	positiveCents = labs(amount.allCents);
	dollars = positiveCents / 100;
	cents = positiveCents % 100;

	if(amount.allCents < 0)
		outs << "-$" << dollars << '.';
	else
		outs << "$" << dollars << '.';

	if(cents < 10)
		outs << '0';
	else
		outs << cents;

	return outs;
}
Your function inside your class says this:

friend ostream& operator <<(ostream& outs, Money &amount);
but your definition is:
ostream& operator <<(ostream &outs, const Money &amount)
Thank you firedraco. I suppose I should be more careful when typing in programs directly from a book. Thanks.
Topic archived. No new replies allowed.