473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with fstream and operator>>


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
6 3192
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
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
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
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
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Armando | last post by:
Hallo ! I habe some error in my programm,because i use <fstream.h>,I want to use <fstream> but i donīt know which fonctions i must modify in my program ? Thanks you for your help. Armando.
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
4
by: Falos425 | last post by:
Okay, I've got a driver.cpp, a section.cpp, and a section.h because I've got a 'section' class. I have an #include <fstream> in the section.h and then section.cpp has an #include "section.h" ...
4
by: adamrobillard | last post by:
Hi, I have always used fopen and FILE* to load and save structures to file. I am trying to convert all the older code to use proper C++ calls... the following code works properly but I would...
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
8
by: khalid302 | last post by:
I need to read a specific number of characters into an std::string from a file regardless of the characters read. * fstream operator>stops when it runs into a white space character. * The global...
6
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.