This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: fstream/ios (hopefully not OT)


Hi Wagner,

You have something really unusual (in an odd / bad way) in your class.  You
shouldn't use the operator << on the ofstream, you should use it on the
ostream.

If you want a binary output (not indicated by your example, but just in
case), you shouldn't use the operator << mechanism, rather you should create
a read method for input, and a write method for output.

My modified version is below, and the problem goes away.

Sincerely,
--Eljay

---- CLASS LISTING
#include <iostream>
    using std::ostream;

class Test
{
private:
    int m;
    int n;

public:
    Test()
    : m(0), n(0) { }

    Test(int a, int b = 0)
    : m(a), n(b) { }
    
    friend ostream& operator << (ostream& ofstr, const Test& a);
};

ostream& operator << (ostream& ofstr, const Test &a)
{
    ofstr << a.m;
    return ofstr;
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]