Answers 859
D
- The function setValue(Animal&,int)was declared to be a friend, but the over-
loaded function setValue(Animal&,int,int)was not declared to be a friend. - 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: void setValue(Animal& ,int,int); // here’s the change!
7:
8: class Animal
9: {
10: friend void setValue(Animal& ,int);
11: friend void setValue(Animal& ,int,int);
12: private:
13: int itsWeight;
14: int itsAge;
15: };
16:
17: void setValue(Animal& theAnimal, int theWeight)
18: {
19: theAnimal.itsWeight = theWeight;
20: }
21:
22: void setValue(Animal& theAnimal, int theWeight, int theAge)
23: {
24: theAnimal.itsWeight = theWeight;
25: theAnimal.itsAge = theAge;
26: }
27:
28: int main()
29: {
30: Animal peppy;
31: setValue(peppy,5);
32: setValue(peppy,7,9);
33: return 0;
34: }
Day 17
Quiz
- The insertion operator (<<) is a member operator of the ostreamobject and is used
for writing to the output device. - The extraction operator (>>) is a member operator of the istreamobject and is
used for writing to your program’s variables.
32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 859