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

Problem with stream operator

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

Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <s@s.com> 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::ostream& operator<<(std::ostream& 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::ofstream 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


Jul 22 '05 #2

P: n/a
I did try overloads with std::ifstream and std::ofstream and I got the same
error.

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:73********************************@4ax.com...
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <s@s.com> 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::ofstream 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

Jul 22 '05 #3

P: n/a
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <s@s.com> 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::ostream& operator<<(std::ostream& 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
Jul 22 '05 #4

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

Jul 22 '05 #5

P: n/a
tom_usenet <to********@hotmail.com> wrote in message news:<mt********************************@4ax.com>. ..
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <s@s.com> 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::ostream& operator<<(std::ostream& 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.
Jul 22 '05 #6

P: n/a
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
kb******@gascad.at
Jul 22 '05 #7

P: n/a
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::ostream& operator<<(std::ostream& 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
kb******@gascad.at
Jul 22 '05 #8

P: n/a
On 2 Jun 2004 07:08:07 -0700, dl**********@yahoo.com (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
Jul 22 '05 #9

P: n/a
Jorge Rivera <jo*****@rochester.rr.com> wrote in message news:<M0*******************@twister.nyroc.rr.com>. ..
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::ostream& operator<<(std::ostream& 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.
Jul 22 '05 #10

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

P: n/a
Jorge Rivera <jo*****@rochester.rr.com> wrote in message news:<pq*******************@twister.nyroc.rr.com>. ..
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.


That was the complete error message.
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.