home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #23 / NN_1992_23.iso / spool / gnu / gcc / help / 2319 < prev    next >
Encoding:
Text File  |  1992-10-13  |  3.0 KB  |  94 lines

  1. Newsgroups: gnu.gcc.help
  2. Path: sparky!uunet!think.com!spool.mu.edu!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!daniel_x
  3. From: daniel_x@athena.mit.edu (Daniel M Higgins)
  4. Subject: g++  classes as friends  more info 
  5. Message-ID: <1992Oct13.142716.11466@athena.mit.edu>
  6. Sender: news@athena.mit.edu (News system)
  7. Nntp-Posting-Host: w20-575-82.mit.edu
  8. Organization: Massachusetts Institute of Technology
  9. References:  <1992Oct13.010907.5696@athena.mit.edu>
  10. Date: Tue, 13 Oct 1992 14:27:16 GMT
  11. Lines: 81
  12.  
  13. --------
  14.  
  15. The compiler I'm using is gcc-2.2.2 and the platform is a DecStation 
  16.  
  17. workstations reaction was
  18.  
  19.  
  20. athena% make -f alt1.mak
  21. /afs/sipb/project/gnu/bin/g++ -g -c -I/afs/sipb/project/gnu/lib/g++-include mons.C
  22. mons.C:13: `DigitStream' is neither function nor method; cannot be declared friend
  23. mons.C: In method `DigitStream::DigitStream (const class BigInt&)':
  24. mons.C:49: member `digits' is a private member of class `BigInt'
  25. mons.C:50: member `ndigits' is a private member of class `BigInt'
  26. *** Error code 1
  27.  
  28. Stop.
  29. athena% make -f alt1.mak
  30. /afs/sipb/project/gnu/bin/gcc -g -c -I/afs/sipb/project/gnu/lib/g++-include mons.C
  31. mons.C:13: `DigitStream' is neither function nor method; cannot be declared friend
  32. mons.C: In method `DigitStream::DigitStream (const class BigInt&)':
  33. mons.C:49: member `digits' is a private member of class `BigInt'
  34. mons.C:50: member `ndigits' is a private member of class `BigInt'
  35. *** Error code 1
  36.  
  37.  
  38.  
  39. code:
  40.  
  41.  
  42. // BigInt.h -- Multiple-precision integer class
  43.  
  44. #include <stdio.h>
  45. #include <iostream.h>
  46.  
  47. class BigInt {
  48.     char* digits;           // pointer to digit array in free store
  49.     unsigned ndigits;       // number of digits
  50.     BigInt(char* d, unsigned n) {   // constructor function
  51.         digits = d;
  52.         ndigits = n;
  53.     }
  54.     friend DigitStream;
  55.     char* scanChunk(istream&);      // read a chunk of digits
  56. public:
  57.     BigInt(const char*);            // constructor function
  58.     BigInt(unsigned n =0);          // constructor function
  59.     BigInt(const BigInt&);          // copy constructor function
  60.     void operator=(const BigInt&);  // assignment
  61.     BigInt operator+(const BigInt&) const;  // addition operator
  62.     BigInt operator*(const BigInt&) const;  // multiplication operator
  63.                                             // function
  64.     void print(FILE* f =stdout) const;      // printing function
  65.     ~BigInt()   { delete digits; }          // destructor function
  66.     void printOn(ostream&) const;           // printing function
  67.     void scanFrom(istream&);                // reading function
  68. };
  69.  
  70.  
  71.  
  72. class DigitStream {
  73.     char* dp;               // pointer to current digit
  74.     unsigned nd;            // number of digits remaining
  75. public:
  76.     DigitStream(const BigInt& n) {  // constructor function
  77.         dp = n.digits;
  78.         nd = n.ndigits;
  79.     }
  80.     unsigned operator++() {         // return current digit
  81.                                     // and advance
  82.         if (nd == 0) return 0;
  83.         else {
  84.             nd--;
  85.             return *dp++;
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91.     Daniel
  92.  
  93. daniel_x@athena.mit.edu
  94.