serialization - Visual C++, CArchive Serialize -


i learn visual c++ visual studio 2010. tried use serialize function of mfc cobject. can't load object serialize function code:

#include <afxwin.h> #include <iostream>  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 &ar);     void print() const;     declare_serial(cmyobject) };  implement_serial(cmyobject, cobject, 1)  void cmyobject::serialize(carchive &ar) {     cobject::serialize(ar);     if (ar.isstoring())         ar << x;     else         ar >> x; }  void cmyobject::print() const {     cout << "cmyobject (" << x << "," << y << ")" << endl; }    int main() {     cmyobject cm(1,3);     cfile files, filel;     files.open(l"c:\\cmyobject.dat", cfile::modewrite | cfile::modecreate);     carchive arstore(&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(&filel, carchive::load);     cm.serialize(arload);     cm.print();     arload.close(); } 

program died on string:

    cm.serialize(arload); 

could tell me what's wrong code?

you should checking calls open() failure. forgot close file after finished writing it. add files.close(); after close archive object.

if(!files.open(l"c:\\source\\cmyobject.dat", cfile::modewrite | cfile::modecreate)) {     std::cout << "unable open output file" << std::endl;     return 1; } carchive arstore(&files, carchive::store); cm.print(); cm.serialize(arstore); arstore.close(); files.close();  //  <--- close file   if(!filel.open(l"c:\\source\\cmyobject.dat", cfile::moderead)) {     std::cout << "unable open input file" << std::endl;     return 1; } carchive arload(&filel, carchive::load); cm.serialize(arload); cm.print(); arload.close(); filel.close();  //  <--- close file 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -