Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with stream operator

Reply
Thread Tools

Problem with stream operator

 
 
David Briggs
Guest
Posts: n/a
 
      06-02-2004
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:stream& operator<<(std: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.



 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      06-02-2004
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <(E-Mail Removed)> wrote:

>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:stream& operator<<(std: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: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

>


 
Reply With Quote
 
 
 
 
David Briggs
Guest
Posts: n/a
 
      06-02-2004
I did try overloads with std::ifstream and std:fstream and I got the same
error.

"Leor Zolman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <(E-Mail Removed)> wrote:
>
> 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: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
>
> >

>



 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      06-02-2004
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <(E-Mail Removed)> wrote:

>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:stream& operator<<(std: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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
Jorge Rivera
Guest
Posts: n/a
 
      06-02-2004
David Briggs wrote:
> 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:stream& operator<<(std: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;
> }
>

 
Reply With Quote
 
David Briggs
Guest
Posts: n/a
 
      06-02-2004
tom_usenet <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <(E-Mail Removed)> wrote:
>
> >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:stream& operator<<(std: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.


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

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


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.

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


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.
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-02-2004
David Briggs wrote:
>
> > >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.

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



--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-02-2004
David Briggs wrote:
>
> >
> > >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.


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:stream& operator<<(std: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 );
}

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      06-02-2004
On 2 Jun 2004 07:08:07 -0700, (E-Mail Removed) (David Briggs)
wrote:

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

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

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


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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
David Briggs
Guest
Posts: n/a
 
      06-02-2004
Jorge Rivera <(E-Mail Removed)> wrote in message news:<M0jvc.32861$(E-Mail Removed)>. ..
> David Briggs wrote:
> > 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:stream& operator<<(std: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;
> > }
> >

Yes I have tried that, it does not help the problem.
I only did it this way to get better error message.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stream operator in namespace masks global stream operator mrstephengross C++ 3 05-10-2007 07:02 AM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments