By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

trouble with >> overload

P: n/a
I've overloaded the istream operator >> to output the private
data members of my class Word, i'm using the operator go get
data from an input file with something like
#include <iostream>
#include <fstream>
#include <cstdlib>
int main(int argc, char* argv[]) {
ifstream in(arcv[1]);
word W;
while(!in.eof()) {
in >> W;
cout << W;
below is the class and overload declarations, the problem is that
if I attempt to read in a file with 16 zeroes in it, the above code
will print 17 zeroes to counsole.
I suspected after looking thru with a debugger, that the blank char
at the end of the file is being read into istream and instantiating
a word object with the default constructor(no args). But when I change
this constructor to
this->wr = 0xfff;
the 17th digit does not become 0xfff or its decimal equvalent, it is
still zero.

Any Ideas as to what is happening here?

class word {
unsigned long wr;
word(void) {
this->wr = 0;
unsigned long value(void) {
return (this->wr);
void set(char* s_wr) {
this->wr = strtoul(s_wr,'\0',16);
extern istream &operator >> (istream &in, word &wr);
extern ostream &operator << (ostream &out, word &wr);
istream &operator >>(istream &in, word &wr) {
char* line;
line = new char[33];
in >> ws;
in >>line;
if (in.bad()) return(in);
delete[] line;
return (in);

ostream &operator <<(ostream &out, word &wr) {
out << hex << wr.value()<<endl;

The opinions expressed in this message are mine and not
those of my employer.
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.