title: Visual C++, CArchive Serialize
body: <p>I learn Visual C++ with Visual Studio 2010.
  I tried to use Serialize function of MFC CObject.
  I can't load my object with Serialize function
  My code:</p>
  
  <pre><code>#include &lt;afxwin.h&gt;
  #include &lt;iostream&gt;
  
  using std::cout;
  using std::endl;
  
  // CMyObject
  
  class CMyObject : public CObject
  {
  public:
      int x, y;
      CMyObject(int _x=0, int _y=0) : CObject(), x(_x), y(_y) {}
      void Serialize(CArchive &amp;ar);
      void Print() const;
      DECLARE_SERIAL(CMyObject)
  };
  
  IMPLEMENT_SERIAL(CMyObject, CObject, 1)
  
  void CMyObject::Serialize(CArchive &amp;ar)
  {
      CObject::Serialize(ar);
      if (ar.IsStoring())
          ar &lt;&lt; x;
      else
          ar &gt;&gt; x;
  }
  
  void CMyObject::Print() const
  {
      cout &lt;&lt; "CMyObject (" &lt;&lt; x &lt;&lt; "," &lt;&lt; y &lt;&lt; ")" &lt;&lt; endl;
  }
  
  
  
  int main()
  {
      CMyObject cm(1,3);
      CFile fileS, fileL;
      fileS.Open(L"C:\\CMyObject.dat", CFile::modeWrite | CFile::modeCreate);
      CArchive arStore(&amp;fileS, CArchive::store);
  
      cm.Print();
      cm.Serialize(arStore);
      arStore.Close();
  
      cm.x = 2;
      cm.Print();
  
      fileL.Open(L"C:\\CMyObject.dat", CFile::modeRead);
      CArchive arLoad(&amp;fileL, CArchive::load);
      cm.Serialize(arLoad);
      cm.Print();
      arLoad.Close();
  }
  </code></pre>
  
  <p>Program died on the string:</p>
  
  <pre><code>    cm.Serialize(arLoad);
  </code></pre>
  
  <p>Could you tell me what's wrong with this code?</p>