Problem with fstream and operator>>

D

David Briggs

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.
 
J

Jorge Rivera

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
 
R

Rolf Magnus

David said:
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 said:
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?
 
D

David Briggs

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

Rolf Magnus

David said:
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.
 
J

John Harrison

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top