Problem with stream operator

Discussion in 'C++' started by David Briggs, Jun 2, 2004.

  1. David Briggs

    David Briggs Guest

    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.
     
    David Briggs, Jun 2, 2004
    #1
    1. Advertising

  2. David Briggs

    Leor Zolman Guest

    On Wed, 02 Jun 2004 03:31:51 GMT, "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::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

    >
     
    Leor Zolman, Jun 2, 2004
    #2
    1. Advertising

  3. David Briggs

    David Briggs Guest

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

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

    >
     
    David Briggs, Jun 2, 2004
    #3
  4. David Briggs

    tom_usenet Guest

    On Wed, 02 Jun 2004 03:31:51 GMT, "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::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
    --
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Jun 2, 2004
    #4
  5. David Briggs

    Jorge Rivera Guest

    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::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;
    > }
    >
     
    Jorge Rivera, Jun 2, 2004
    #5
  6. David Briggs

    David Briggs Guest

    tom_usenet <> wrote in message news:<>...
    > On Wed, 02 Jun 2004 03:31:51 GMT, "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::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.


    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.
     
    David Briggs, Jun 2, 2004
    #6
  7. 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
     
    Karl Heinz Buchegger, Jun 2, 2004
    #7
  8. 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::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 );
    }

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 2, 2004
    #8
  9. David Briggs

    tom_usenet Guest

    On 2 Jun 2004 07:08:07 -0700, (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
     
    tom_usenet, Jun 2, 2004
    #9
  10. David Briggs

    David Briggs Guest

    Jorge Rivera <> wrote in message news:<M0jvc.32861$>...
    > 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::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;
    > > }
    > >

    Yes I have tried that, it does not help the problem.
    I only did it this way to get better error message.
     
    David Briggs, Jun 2, 2004
    #10
  11. David Briggs

    Jorge Rivera Guest

    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.
     
    Jorge Rivera, Jun 3, 2004
    #11
  12. David Briggs

    David Briggs Guest

    Jorge Rivera <> wrote in message news:<pqvvc.286119$>...
    > 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.
     
    David Briggs, Jun 3, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmusson, Lars
    Replies:
    1
    Views:
    782
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    671
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    476
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    595
    Thomas Fritsch
    Mar 18, 2007
  5. mrstephengross
    Replies:
    3
    Views:
    418
    James Kanze
    May 10, 2007
Loading...

Share This Page