how do I make fstream's >> work for my class?

This is a discussion on how do I make fstream's >> work for my class? within the C++ Programming forums, part of the General Programming Boards category; hi, it's me again I've got a crazy idea, which I am trying to get working. I've coded a class ...

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    how do I make fstream's >> work for my class?

    hi, it's me again

    I've got a crazy idea, which I am trying to get working. I've coded a class called Float. it looks something like this:

    Code:
    class Float
    {
    public:
    Float();
    ~Float();
    
    // here come some operators
    //...
    
    //at last the value of the float
    float Value;
    }
    everything is working fine besides one small detail. When I tried to do something like:

    Code:
    Float SomeFloat;
    ifstream SomeFile("my_file");
    
    SomeFile>>SomeFloat;
    I got a lot of errors. That was really not surprising, since fstream (or, in this case ifstream) hasn't got an operator>> for my class Float.

    My question is, can I make fstream (and ifstream) somehow recognize my Float class so that I can simply do SomeFile>>SomeFloat?

    At the same time I want to avoid things like:
    Code:
    SomeFile>>SomeFloat.Value;
    (it looks rather nasty and besides that I want to keep Value protected)

    thnx
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Implement these operators for text-based I/O. They need to be global, not inside your class. However, you might need to make them friends of your class in order to access the internals.
    (First the simple version)
    Code:
    std::ostream & operator <<(std::ostream &os, const Float &fl); // Output
    std::istream & operator >>(std::istream &is, Float &fl); // Input
    (And now the full-blow complete version)
    Code:
    template < typename C, class Traits >
    std::basic_ostream<C, Traits> & operator <<(std::basic_ostream<C, Traits> &os, const Float &fl); // Output
    template < typename C, class Traits >
    std::basic_istream<C, Traits> & operator >>(std::basic_istream<C, Traits> &is, Float &fl); // Input
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Overload the stream extraction operator for your class. Remember to return a reference to an istream.

    edit: Foiled again!
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    thanks a lot, I'll try it out as soon as I come home
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Can you make an std::list of a templated class.
    By chadsxe in forum C++ Programming
    Replies: 39
    Last Post: 02-12-2009, 02:04 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Replies: 8
    Last Post: 10-02-2005, 01:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21