473,715 Members | 6,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetBuffe r( 10 );
fs >> p;
x.Name.ReleaseB uffer(-1);
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnF ileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.Get PathName());
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_ifst ream<char,struc t std::char_trait s<char> >'

I don't know how to fix this.

Jul 22 '05 #1
11 2291
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.GetBuffe r( 10 );
fs >> p;
x.Name.ReleaseB uffer(-1);
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnF ileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.Get PathName());
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_ifst ream<char,struc t std::char_trait s<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
"convention al" << 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.co m> wrote in message
news:73******** *************** *********@4ax.c om...
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
"convention al" << 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.GetBuffe r( 10 );
fs >> p;
What if the string is longer than 10 chars? You have undefined
behaviour I think.
x.Name.ReleaseB uffer(-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::OnF ileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.Get PathName());
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_ifst ream<char,struc t std::char_trait s<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.GetBuffe r( 10 );
fs >> p;
x.Name.ReleaseB uffer(-1);
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnF ileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.Get PathName());
operator>>(fs, myData);
Replace this line with
fs>>myData; }

Jul 22 '05 #5
tom_usenet <to********@hot mail.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.GetBuffe r( 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.ReleaseB uffer(-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::OnF ileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.Get PathName());
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_ifst ream<char,struc t std::char_trait s<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.GetBuffe r( 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_ifst ream<char,struc t std::char_trait s<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.GetBuffe r( 10 );
fs >> p;
x.Name.ReleaseB uffer(-1);
return fs;
};

int main()
{
Data myData;

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

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #8
On 2 Jun 2004 07:08:07 -0700, dl**********@ya hoo.com (David Briggs)
wrote:
>std::istream & operator>>( std::istream& fs, Data& x )
>{
> LPTSTR p = x.Name.GetBuffe r( 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*****@roches ter.rr.com> wrote in message news:<M0******* ************@tw ister.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.GetBuffe r( 10 );
fs >> p;
x.Name.ReleaseB uffer(-1);
return fs;
};

Now when I go to use this in my dialog:

void CIotestDlg::OnF ileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.Get PathName());
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
37908
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 thought I would try <fstream>. The code below causes an infinite loop in the while() statement. Debugging, I see that the buffer just has a 0 in the first position, and nothing else.
4
3141
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 manipulate a data stream by using a custom streambuf, so that I can access this as "just another iostream". Alas, this data stream has a couple of "interesting" properties:
0
2072
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 templating completely everything works perfectly. Here is the error message: Error: Unresolved external 'operator <<(std::basic_ostream<char, std::char_traits<char> >&, const BinomialTree<int>&)' referenced from C:\DOCUMENTS AND...
2
5965
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 get the following error: Error: Unresolved external 'operator <<(std::basic_ostream<char, std::char_traits<char> >&, const BinomialTree<int>&)' referenced from C:\DOCUMENTS AND SETTINGS\RYAN\DESKTOP\COMPUTER SCIENCE\CS
0
1832
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 be disabled in a release build, and to do that efficiently, I suppose I need to overload the << operator. My current implementation of the stream in release mode is posted below. Everything works fine for POD type like bool and int, but the...
4
1641
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 be able to point me in the right direction. Ok, I was trying to debug some code where endl appearantly wasn't doing it's job, or at least, did not seem to be inserting an '\n' into the stream. I managed to crystalize the problem area into...
3
3032
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 buffer etc, but nothing worked. Ideas? Also might use another internal construct, only API is needed and requirement to reuse existing std::ostream inserters.
4
1178
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
1544
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 is no header file to include, but I can't seem to figure out how to output the data to the file I create when the main is in a different. Will someone give me an example or show me what I am doing wrong? #include <iostream> #include...
9
1924
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 consist of one or more words and a few integers. I'm able to create the file properly, as confimed by viewing it in a text editor, but something goes wrong when I tried to read it. I've made a test program illustrating the problem: #include...
0
8821
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9047
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.