Problem with fstream and operator>>

Discussion in 'C++' started by David Briggs, May 23, 2004.

  1. David Briggs

    David Briggs Guest

    I am using MS VC++ 6.0 with MFC
    I have a simple class:

    #include <fstream.h>

    class Data
    {
    public:
    CString WriteStr();
    Data();
    virtual ~Data();
    CString Name;
    };

    fstream& operator<<(fstream& fs, Data& x )
    {
    fs << x.Name;
    return fs;
    };

    fstream& operator>>( fstream& fs, Data& x )
    {
    fs >> x.Name;
    return fs;
    };

    I have been having many problem with this. Sometime I get link errors
    with just the code for operator<< and sometimes I don't. When I did get the
    operator<< working I added operator>> . Now when I compiled this I get the
    following error:

    error C2678: binary '>>' : no operator defined which takes a left-hand
    operand of type 'class fstream' (or there is no acceptable conversion)
    I did try #include <fstream> and that did not work at all.

    Has anyone run into this type of problem?

    thanks.
    David Briggs, May 23, 2004
    #1
    1. Advertising

  2. David Briggs

    Jorge Rivera Guest

    Try
    ofstream& operator<<(ofstream& fs, const Data& x )
    ifstream& operator>>(ifstream& fs, Data& x )

    I'm not sure if this will help your problem or not, but it might...

    JLR
    Jorge Rivera, May 23, 2004
    #2
    1. Advertising

  3. David Briggs

    Rolf Magnus Guest

    David Briggs wrote:

    >
    > I am using MS VC++ 6.0 with MFC
    > I have a simple class:
    >
    > #include <fstream.h>


    <fstream.h> is a non-standard header. Use <fstream> instead. But since
    there is no reason to limit yourself to fstreams, you should actually
    do:

    #include <istream>
    #include <ostream>

    > class Data
    > {
    > public:
    > CString WriteStr();
    > Data();
    > virtual ~Data();
    > CString Name;
    > };
    >
    > fstream& operator<<(fstream& fs, Data& x )


    std::eek:stream& operator<<(std::eek:stream& fs, const Data& x )

    > {
    > fs << x.Name;
    > return fs;
    > };
    >
    > fstream& operator>>( fstream& fs, Data& x )


    std::istream& operator>>(std::istream& fs, Data& x )
    > {
    > fs >> x.Name;
    > return fs;
    > };
    >
    > I have been having many problem with this. Sometime I get link
    > errors
    > with just the code for operator<< and sometimes I don't.


    What did they say?

    > When I did get the operator<< working I added operator>> . Now when I
    > compiled this I get the following error:
    >
    > error C2678: binary '>>' : no operator defined which takes a
    > left-hand operand of type 'class fstream' (or there is no
    > acceptable conversion)


    How did you try to use it?

    > I did try #include <fstream> and that did not work at all.
    >
    > Has anyone run into this type of problem?
    >
    > thanks.


    --
    Never wondered why a carrot is more orange than an orange?
    Rolf Magnus, May 24, 2004
    #3
  4. David Briggs

    Michael Guest

    did you manage to solve them, I'm getting the same!


    "David Briggs" <> wrote in message
    news:8F7sc.3175$...
    >
    > I am using MS VC++ 6.0 with MFC
    > I have a simple class:
    >
    > #include <fstream.h>
    >
    > class Data
    > {
    > public:
    > CString WriteStr();
    > Data();
    > virtual ~Data();
    > CString Name;
    > };
    >
    > fstream& operator<<(fstream& fs, Data& x )
    > {
    > fs << x.Name;
    > return fs;
    > };
    >
    > fstream& operator>>( fstream& fs, Data& x )
    > {
    > fs >> x.Name;
    > return fs;
    > };
    >
    > I have been having many problem with this. Sometime I get link

    errors
    > with just the code for operator<< and sometimes I don't. When I did get

    the
    > operator<< working I added operator>> . Now when I compiled this I get the
    > following error:
    >
    > error C2678: binary '>>' : no operator defined which takes a

    left-hand
    > operand of type 'class fstream' (or there is no acceptable conversion)
    > I did try #include <fstream> and that did not work at all.
    >
    > Has anyone run into this type of problem?
    >
    > thanks.
    >
    >
    Michael, May 24, 2004
    #4
  5. David Briggs

    David Briggs Guest

    I did get it to work.
    I change #include <fstream.h> to #include <fstream>
    Nest I added the prefix std:: And then I added GetBuffer for the
    CString input. I have yet to test the code but for now it compiled.



    std::fstream& operator<<(std::fstream& fs, const Data& x )
    {
    fs << x.Name;
    return fs;
    };

    std::fstream& operator>>( std::fstream& fs, Data& x )
    {
    LPTSTR p = x.Name.GetBuffer( 10 );
    fs >> p;
    x.Name.ReleaseBuffer(-1);
    return fs;
    };
    David Briggs, May 26, 2004
    #5
  6. David Briggs

    Rolf Magnus Guest

    David Briggs wrote:

    > I did get it to work.
    > I change #include <fstream.h> to #include <fstream>
    > Nest I added the prefix std:: And then I added GetBuffer for the
    > CString input. I have yet to test the code but for now it compiled.
    >
    >
    >
    > std::fstream& operator<<(std::fstream& fs, const Data& x )
    > {
    > fs << x.Name;
    > return fs;
    > };
    >
    > std::fstream& operator>>( std::fstream& fs, Data& x )
    > {
    > LPTSTR p = x.Name.GetBuffer( 10 );
    > fs >> p;
    > x.Name.ReleaseBuffer(-1);
    > return fs;
    > };


    I'd like to ask you again why you want to restrict yourself to fstream
    for input and output. It will mean that you cannot read a Data object
    from an ifstream or write one to a stringstream or some stream that's
    not part of the standard library, and I don't see any reason for
    forbidding this. Always use a reference to istream for reading and one
    to ostream for writing.
    Rolf Magnus, May 26, 2004
    #6
  7. >
    > I'd like to ask you again why you want to restrict yourself to fstream
    > for input and output. It will mean that you cannot read a Data object
    > from an ifstream or write one to a stringstream or some stream that's
    > not part of the standard library, and I don't see any reason for
    > forbidding this. Always use a reference to istream for reading and one
    > to ostream for writing.
    >


    It's likely to be simple enough. Many newbies don't appreciate that using an
    istream for input allows you to read from any input stream, including
    fstream. Similarly for ostream and output streams.

    David, replace fstream with istream in operator>> and ostream in operator<<.

    john
    John Harrison, May 26, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Armando
    Replies:
    6
    Views:
    745
    Armando
    Jan 29, 2004
  2. Replies:
    5
    Views:
    530
    James Kanze
    Apr 2, 2007
  3. Replies:
    6
    Views:
    337
    Martin York
    Jun 21, 2008
  4. Replies:
    8
    Views:
    389
  5. Sachin Garg
    Replies:
    1
    Views:
    483
    Ron AF Greve
    Aug 22, 2008
Loading...

Share This Page