Problem with stream operator

D

David Briggs

I am using MFC VC++ 6.0
and I have a simple class 'Data' with a CString member var. Name.
I added operator << and >> as:

#include <istream>
#include <ostream>
#include <fstream>

std::eek:stream& operator<<(std::eek:stream& fs, const Data& x )
{
fs << x.Name;
return fs;
};

std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;
x.Name.ReleaseBuffer(-1);
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnFileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs, myData);
}
UpdateData( FALSE );
}


I get this error. (I also tried just fs >> myData;)
error C2665: '>>' : none of the 22 overloads can convert parameter 1 from
type 'class std::basic_ifstream<char,struct std::char_traits<char> >'

I don't know how to fix this.
 
L

Leor Zolman

I am using MFC VC++ 6.0
and I have a simple class 'Data' with a CString member var. Name.
I added operator << and >> as:

#include <istream>
#include <ostream>
#include <fstream>

std::eek:stream& operator<<(std::eek:stream& fs, const Data& x )
{
fs << x.Name;
return fs;
};

std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;
x.Name.ReleaseBuffer(-1);
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnFileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs, myData);
}
UpdateData( FALSE );
}


I get this error. (I also tried just fs >> myData;)
error C2665: '>>' : none of the 22 overloads can convert parameter 1 from
type 'class std::basic_ifstream<char,struct std::char_traits<char> >'

I don't know how to fix this.

I have a vague recollection of dealing with this in VC6, and finally
learning (from someone at Dinkumware, if I recall) that there's no
real fix...I think I just ended up overloading operators << and >> to
take a std::ifstream and std::eek:fstream in addition to the
"conventional" << and >> overloads. There may be a better solution,
but it may not be worth the trouble to try to figure it out. If you
do, and you find such a solution, let us know what you find ;-)
-leor
 
D

David Briggs

I did try overloads with std::ifstream and std::eek:fstream and I got the same
error.
 
T

tom_usenet

I am using MFC VC++ 6.0
and I have a simple class 'Data' with a CString member var. Name.
I added operator << and >> as:

#include <istream>
#include <ostream>
#include <fstream>

std::eek:stream& operator<<(std::eek:stream& fs, const Data& x )
{
fs << x.Name;
return fs;
};

std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;

What if the string is longer than 10 chars? You have undefined
behaviour I think.
x.Name.ReleaseBuffer(-1);

Wouldn't it be better to write an operator>> for CString too? Read the
characters 1 at a time and append them (checking for whitespace).
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnFileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs, myData);

That's normally written fs >> myData;

Is myData a non-const member of CIotestDlg of type Data?

}
UpdateData( FALSE );
}

Does the OnFileOpen definition really appear just below the operator>>
definitions? Or are only some friend declarations inside the Data
class definition visible?
I get this error. (I also tried just fs >> myData;)
error C2665: '>>' : none of the 22 overloads can convert parameter 1 from
type 'class std::basic_ifstream<char,struct std::char_traits<char> >'

I don't know how to fix this.

Looks like a name lookup issue. The simple change above might well fix
it. Otherwise, a minimal complete example would make it easier to fix.
If you only have friend declarations, you should also declare the
operator>> functions outside the class declaration for Data.

Tom
 
J

Jorge Rivera

David said:
I am using MFC VC++ 6.0
and I have a simple class 'Data' with a CString member var. Name.
I added operator << and >> as:

#include <istream>
#include <ostream>
#include <fstream>

std::eek:stream& operator<<(std::eek:stream& fs, const Data& x )
{
fs << x.Name;
return fs;
};

std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;
x.Name.ReleaseBuffer(-1);
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnFileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs, myData);

Replace this line with
fs>>myData;
 
D

David Briggs

tom_usenet said:
What if the string is longer than 10 chars? You have undefined
behaviour I think.

Name is a MFC CString and will handle the space if there is more then 10
chars.
Wouldn't it be better to write an operator>> for CString too? Read the
characters 1 at a time and append them (checking for whitespace).


That's normally written fs >> myData;

Yes I know, I only call >> this way to get a more
description in the error message.
Is myData a non-const member of CIotestDlg of type Data?

Yes it is.
Does the OnFileOpen definition really appear just below the operator>>
definitions? Or are only some friend declarations inside the Data
class definition visible?


Looks like a name lookup issue. The simple change above might well fix
it. Otherwise, a minimal complete example would make it easier to fix.
If you only have friend declarations, you should also declare the
operator>> functions outside the class declaration for Data.

Tom

The function OnFileOpen() is part of a dialog class and is
not part of the Data class. The operator>> and << are function
define outside of any class.
 
K

Karl Heinz Buchegger

David said:
Name is a MFC CString and will handle the space if there is more then 10
chars.

No it will not. You are not reading into Name. You are reading into
a character array denoted by p. It is just that you get this character
array by asking Name for a character array with a size of 10 characters.
And that's what CString is doing: adjusting its internal buffer to 10 characters
and giving you a pointer to it. If you read more then 10 characters -> kaboom.
 
K

Karl Heinz Buchegger

Hmm. Wasn't there some 'friend' issue with operator overloading
which has been fixed by one of the service packs.

Anyway: Can you try if the following compiles for you.
I added a dummy CString class plus a definition for LPTSTR
to make it compileable. It compilers fine for me

#include <istream>
#include <ostream>
#include <fstream>

#define LPTSTR char*

class CString
{
public:
char* GetBuffer( int i ) { return p; }
void ReleaseBuffer( int i ) {}
operator const char* () const { return p; }

protected:
char p[40];
};

class Data
{
public:
CString Name;
};

std::eek:stream& operator<<(std::eek:stream& fs, const Data& x )
{
fs << x.Name;
return fs;
};

std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;
x.Name.ReleaseBuffer(-1);
return fs;
};

int main()
{
Data myData;

std::ifstream fs;
fs.open( "C:\test.dat" );
operator>>( fs, myData );
}
 
T

tom_usenet

Name is a MFC CString and will handle the space if there is more then 10
chars.

No, it won't. p is just a pointer to the internal buffer of the Name
variable, and that buffer is only 10 chars long. Write past the end,
and the CString can't do anything about it.
The function OnFileOpen() is part of a dialog class and is
not part of the Data class. The operator>> and << are function
define outside of any class.

I asked where they are declared, not where they are defined. So they
aren't friend functions of anything? Where are they declared? Are they
even visible to the definition of OnFileOpen?

Tom
 
D

David Briggs

Jorge Rivera said:
Replace this line with
fs>>myData;
Yes I have tried that, it does not help the problem.
I only did it this way to get better error message.
 
J

Jorge Rivera

Can you put the complete error message?
I get this error. (I also tried just fs >> myData;)
error C2665: '>>' : none of the 22 overloads can convert parameter 1
from
type 'class std::basic_ifstream<char,struct std::char_traits<char> >'

After this, you must be getting something like ' to ......'
I am wondering what it thinks is should be getting.

Jorge L.
 
D

David Briggs

Jorge Rivera said:
Can you put the complete error message?

type 'class std::basic_ifstream<char,struct std::char_traits<char> >'

After this, you must be getting something like ' to ......'
I am wondering what it thinks is should be getting.

Jorge L.

That was the complete error message.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top