fstream/ios (hopefully not OT)

John Love-Jensen eljay@adobe.com
Thu Jan 30 12:50:00 GMT 2003


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;
}




More information about the Gcc-help mailing list