home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: gnu.gcc.help
- Path: sparky!uunet!think.com!spool.mu.edu!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!daniel_x
- From: daniel_x@athena.mit.edu (Daniel M Higgins)
- Subject: g++ classes as friends more info
- Message-ID: <1992Oct13.142716.11466@athena.mit.edu>
- Sender: news@athena.mit.edu (News system)
- Nntp-Posting-Host: w20-575-82.mit.edu
- Organization: Massachusetts Institute of Technology
- References: <1992Oct13.010907.5696@athena.mit.edu>
- Date: Tue, 13 Oct 1992 14:27:16 GMT
- Lines: 81
-
- --------
-
- The compiler I'm using is gcc-2.2.2 and the platform is a DecStation
-
- workstations reaction was
-
-
- athena% make -f alt1.mak
- /afs/sipb/project/gnu/bin/g++ -g -c -I/afs/sipb/project/gnu/lib/g++-include mons.C
- mons.C:13: `DigitStream' is neither function nor method; cannot be declared friend
- mons.C: In method `DigitStream::DigitStream (const class BigInt&)':
- mons.C:49: member `digits' is a private member of class `BigInt'
- mons.C:50: member `ndigits' is a private member of class `BigInt'
- *** Error code 1
-
- Stop.
- athena% make -f alt1.mak
- /afs/sipb/project/gnu/bin/gcc -g -c -I/afs/sipb/project/gnu/lib/g++-include mons.C
- mons.C:13: `DigitStream' is neither function nor method; cannot be declared friend
- mons.C: In method `DigitStream::DigitStream (const class BigInt&)':
- mons.C:49: member `digits' is a private member of class `BigInt'
- mons.C:50: member `ndigits' is a private member of class `BigInt'
- *** Error code 1
-
-
-
- code:
-
-
- // BigInt.h -- Multiple-precision integer class
-
- #include <stdio.h>
- #include <iostream.h>
-
- class BigInt {
- char* digits; // pointer to digit array in free store
- unsigned ndigits; // number of digits
- BigInt(char* d, unsigned n) { // constructor function
- digits = d;
- ndigits = n;
- }
- friend DigitStream;
- char* scanChunk(istream&); // read a chunk of digits
- public:
- BigInt(const char*); // constructor function
- BigInt(unsigned n =0); // constructor function
- BigInt(const BigInt&); // copy constructor function
- void operator=(const BigInt&); // assignment
- BigInt operator+(const BigInt&) const; // addition operator
- BigInt operator*(const BigInt&) const; // multiplication operator
- // function
- void print(FILE* f =stdout) const; // printing function
- ~BigInt() { delete digits; } // destructor function
- void printOn(ostream&) const; // printing function
- void scanFrom(istream&); // reading function
- };
-
-
-
- class DigitStream {
- char* dp; // pointer to current digit
- unsigned nd; // number of digits remaining
- public:
- DigitStream(const BigInt& n) { // constructor function
- dp = n.digits;
- nd = n.ndigits;
- }
- unsigned operator++() { // return current digit
- // and advance
- if (nd == 0) return 0;
- else {
- nd--;
- return *dp++;
- }
- }
- }
-
-
- Daniel
-
- daniel_x@athena.mit.edu
-