By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Problem with fstream and operator>>

P: n/a

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.
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
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::ostream& operator<<(std::ostream& 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?

Jul 22 '05 #3

P: n/a
did you manage to solve them, I'm getting the same!
"David Briggs" <dl******@flash.net> wrote in message
news:8F*****************@newssvr32.news.prodigy.co m...

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.

Jul 22 '05 #4

P: n/a
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;
};
Jul 22 '05 #5

P: n/a
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.

Jul 22 '05 #6

P: n/a
>
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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.