Overloading >> operator

Does anyone know how I would overload the >> operator so I can insert bytes from a inputbyte stream class and have it insert into another class, such as my counter class which counts the number of times a byte appears? I've tried multiple variations but none work. Heres my two classes:

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
class ibstream {
public:

   /** Default constructor.  Constructs input bit-stream that is not
    *  usable.
    *
    * @post Unusable but initialized bit-stream object. */
   ibstream();

   /** Construct input bit-stream bound to specified file.
    *
    * @post Input bit-stream tied to file specified by name. */
   ibstream(const string&);

   /** Destructor
    *
    * @post Close and free up memory. */
   virtual ~ibstream();

   /** Bind input bit-stream to file specified.  Previously open
    *  bit-stream closed.
    *
    * @post Input bit-stream set to file specified by name.  Any open
    *       bit-stream is closed.
    *
    * @param name Filename.
    *
 bool open(const string& name);

   /** Reads number of bits specified by first parameter and returns
    *  via reference (second) parameter these bits (stored in an int).
    *
    * @post numbits bits read (buffered) and returned as retval.
    *
    * @param numbits Number of bits to read.
    *
    * @param retval Bits read in an int.
    *
    * @return True if numbits were read, otherwise returns false.
    *
    * @throw NoneThrown Maximum number of bits that can be read is
    *                   32. */
   bool readbits(int numbits,
                 int& retval);

   /** Effectively flushes bits buffered during read operations so
   void resetbits();

   /** Reset input bit-stream to beginning, clear it.
    *
    * @post Rewind to beginning of input. */
   void rewind();

   /** Closes stream properly (flushes too).
    *
    * @post Close the input bit-stream. */
   void close();
private:

   /** Stream for reading bits. */
   ifstream* myInputStream;

   /** Buffer bits for input. */

   /** Buffer bits for input. */
   int myInBuff;

   /** Used for buffering. */
   int myInbbits;
};





Counter class:
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
class Counter {
public:

   static const int SIZE = ALPH_SIZE + 1;

   Counter() throw(OutOfStorageException);

   virtual ~Counter();


 inline void increment(int index)
      throw(OutOfStorageException,
            OutOfBoundsException) {

      if (index >= 0 && index < SIZE) {
         // If the items[index] pointer is not set, then set it...
         if (items[index] == 0) {
            try {
               // Set items[index] to the address of a new ByteInfo
               // with value = index, frequency = 1 (as a long long
               // int - the LL), and code set to the empty string.
               items[index] = new ByteInfo(index, 1LL, "");
            }
            catch (const bad_alloc&) {
               throw OutOfStorageException("Out of memory");
            }
         }
         else {
            // Otherwise just increment the frequency of the byte index.
            items[index]->incrementFreq();
         }
      }
      else {
         throw OutOfBoundsException("Index out of range.");
 inline ByteInfo* get(int index) const {

      ByteInfo* item = 0;

      if (index >= 0 && index < SIZE) {
         item = items[index];
      }

      return item;
   }

private:

   /** An array of SIZE pointers to ByteInfo objects. */
   ByteInfo** items;
};
Last edited on
some help would be nice I really have been trying I get weird stream errors
Topic archived. No new replies allowed.