37: return theStream;
38: }
39:
40: istream& operator>>( istream& theStream,String& theString)
41: {
42: theStream >> theString.GetString();
43: return theStream;
44: }
45:
46: int main()
47: {
48: String theString(“Hello world.”);
49: cout << theString;
50: return 0;
51: }
- You can’t put the frienddeclaration into the function. You must declare the func-
tion to be a friend in the class. - The following is the fixed listing:
0: Bug Busters
1: #include
2: using namespace std;
3: class Animal;
4:
5: void setValue(Animal& , int);
6:
7: class Animal
8: {
9: public:
10: friend void setValue(Animal&, int);
11: int GetWeight()const { return itsWeight; }
12: int GetAge() const { return itsAge; }
13: private:
14: int itsWeight;
15: int itsAge;
16: };
17:
18: void setValue(Animal& theAnimal, int theWeight)
19: {
20: theAnimal.itsWeight = theWeight;
21: }
22:
23: int main()
24: {
25: Animal peppy;
26: setValue(peppy,5);
27: return 0;
28: }
858 Appendix D
32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 858