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:
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;
};
class Counter {
public:
staticconstint SIZE = ALPH_SIZE + 1;
Counter() throw(OutOfStorageException);
virtual ~Counter();
inlinevoid 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;
};