Thread: Does overloaded istream operators overload ifstream as well?

  1. #1
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Does overloaded istream operators overload ifstream as well?

    Hey, all! It's been a while. I am having a strange problem which doesn't seem right. I have a class I created that I am trying overload the insertion operator (>>). I do so with this code:

    Code:
    istream &operator>>(istream &in, Student &studentcopy)
    {
        in >> studentcopy.ssn >> studentcopy.creditsToDate >> studentcopy.pointsToDate;
    
        return in;
    }
    Inside the main/driver program code I open and read the data from a file. However, when I use the insertion op (>>), it doesn't read anything from the file. It will read the data in from the keyboard (i.e. it overloads fine with cin), however it gets nothing from a file. The file opens fine and the data is structured ok. I always thought that an ifstream object was also considered an istream.

    I also created another overloaded operator function but instead of istream I made it ifstream. Still nothing, though. Anyone have any ideas? Thanks

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    have you included fstream, and done something like " infile >> " without the quotes? show some code.

    edit: for show code, i mean, like main.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What does your file look like? Remember >> will read up to a whitespace.

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    The file is actually only one line long for now and is like so:

    2 123456789 25 100

    and the main code is like so:

    Code:
    ......
    
    inFile >> iCode;           // an integer
    
    inFile >> sStudent;     // long int, int, int
    
    ......

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>The file is actually only one line long for now and is like so:

    >>2 123456789 25 100

    The file doesnt look like that to the eye though right? If so, then you are trying to read a textfile and assign the data to an int or long......if the real numbers are stored in a file, you whould not be able to view them in a text editor

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Yes, you can read the file (i.e. its a text file). It works fine now, though. It was an old bug in MSVC++ (I never updated). I compiled through g++ and it worked ok.

    Fordy, I think you are thinking of binary-file-I/O. I'm not doing that. You can read from a text file and assign it to ints and such (the standard library takes care of that).

    Thanks anyway guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. using templates to overload operators
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2006, 10:18 PM
  3. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  4. istream overload
    By Trauts in forum C++ Programming
    Replies: 6
    Last Post: 05-06-2003, 08:29 PM
  5. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM