1

I checked the link Can I overload CArchive << operator to work with std::string? about overloading for string type and I used

CArchive& operator<<( CArchive& ar, string lhs)
{
    int size_t = lhs.size();
    ar << size_t;
    ar.Write( lhs.c_str(), size_t );
    return ar;

}

CArchive& operator>>( CArchive& ar, string lhs)
{
     int size_t = lhs.size();
     ar >> size_t;
     lhs.resize(size);
     ar.Read(&lhs[0], size);
}

And I declared the overloaded operators in my header function as a friend.

class CArrow : public CObject  
{
    DECLARE_SERIAL(CArrow)
    friend CArchive& operator<<( CArchive& ar, std::string lhs);
    friend CArchive& operator>>( CArchive& ar, std::string lhs);
  ...
  ...
}

But the >> operator gives me blank output. When I don't use as an operator, just read and write in my serialization function, they work for only one string variable. However, I have more than one string to store and load. Is there any other way to serialize the string type? or What is the problem with this overloaded functions?

Community
  • 1
  • 1
Ugur Sahin
  • 47
  • 1
  • 8
  • Your >> operator needs to specify a reference to a string! And your << operator needs to specify a const reference. – user1793036 Mar 26 '14 at 00:27
  • It worked. Please write it down this and I will accept as an answer. But I didn't understand why, Could you explain it for me, please? – Ugur Sahin Mar 26 '14 at 06:19

1 Answers1

0

Your operator >> seems to be writing the size to ar instead of reading it from ar.

You should add the MFC tag to your question.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15