473,396 Members | 1,797 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,396 software developers and data experts.

Problem with stream operator

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

Similar topics

11
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I...
4
by: Matt Chaplain | last post by:
Hi there. I'm writing a program that uses the Telnet protocol over TCP/IP sockets. Of course, that has no bearing here, so I'll rephrase that in Standard C++ :) In essense, I'm trying to...
0
by: Ryan M. Keith | last post by:
I am having a problem with the ostream operator in templated classes that I wrote (I'm using the Borland compiler), and I'm certain that the templates are the problem because when I remove the...
2
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
4
by: ricardw | last post by:
Hi! I've tried to find an answer to this in the FAQ and elsewhere on the net, without success. I'm fairly sure that the issue must have turned up before and that at least a handful of people may...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
4
by: iman.sharafodin | last post by:
Hello all u guys out there Ive got a problem Undrestanding in example what it means class point { .... }
7
by: B. Williams | last post by:
I wrote a program that would simply output data to the screen, but now I am trying to have the data saved to a file. I don't have a problem creating the file or even outputting data to it when there...
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.