Problem with CList Serializion
Pertti Anttila -- pertti.anttila@lakk.fi
Wednesday, July 17, 1996
I have a big trouble with CList Serializion!
============================================
I've made a program that holds most of it's data in a CList object,
but now I can't save that data to disk with serializion properly.
So I made a little test program just for testing. I made two list:
CList int_list;
CList string_list;
void CMySerialDoc::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
int_list.Serialize(ar);
string_list.Serialize(ar);
}
These lists works just fine. I can use them as I expect, no
problems with serializion.
Next I made a class containing an integer and a string. And
a list containing objects of that type.
CList class_list;
void CMySerialDoc::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
int_list.Serialize(ar);
string_list.Serialize(ar);
class_list.Serialize(ar);
}
When I build my application, every thing works just fine: no errors,
no warnings. On the window every list works just fine. When I save
the document to disk, still okey. But when I start the program again,
everything falls down.
The program gives an exception:
--- Error message: ---
Assertion Failed!
MySerial: File afxmem.cpp Line 367
So there is something wrong with my class! But I don't know what.
Please, could anyone tell me, how to make the next class so, that I
can serialize it's objects to disk file with CList object:
// DECLARATION
class CTesti : public CObject // declaration, inherited from CObject
{
public: // attributes
CString nimi;
int numero;
public:
CTesti() {} // default constructor, empty
virtual ~CTesti() {}; // virtual destructor, empty
// ( no new/delete )
void Serialize(CArchive & ar); // override Serialize()
CTesti& operator=(CTesti &joku);// needed in
//class_list->AddTail()
DECLARE_SERIAL(CTesti); // I thougth this is all you need...
};
// IMPLEMENTATION
IMPLEMENT_SERIAL(CTesti, CObject, 0); // At least allmost all...
CTesti& CTesti::operator=(CTesti &joku)
{
nimi = joku.nimi;
numero = joku.numero;
return *this;
}
void CTesti::Serialize(CArchive & ar)
{
if (ar.IsStoring())
{
ar << nimi;
ar << (WORD)numero;
}
else
{
WORD apu;
ar >> nimi;
ar >> apu;
numero = apu;
}
}
Environment: VC++ 2.0 / Win95
Greetings from Finland
Pertti Anttila
Mike Blaszczak -- mikeblas@msn.com
Sunday, July 21, 1996
> I've made a program that holds most of it's data in a CList object,
> but now I can't save that data to disk with serializion properly.
First, you're calling CObject::Serialize() in your CDocument-derived class
Serialize() implementation. I think you should call CDocument::Serialize().
This probably isn't the cause of your problem, though: the cause of your
problem is failing to read the documentation.
> Please, could anyone tell me, how to make the next class so, that I
> can serialize it's objects to disk file with CList object:
You need to provide "helper" functions for your template implementation. Since
you're not providing such helpers, MFC is calling default implementations of
those functions and they're just doing shallow, bitwise serializations of the
classes involved. You need to make sure that you implement CreateElements()
and SerializeElements() and probably DesroyElements() helpers for your
particular class to make sure it works right.
My book has lots of text on this, as do other books on MFC. The product
documentation has information on it, too: there's no shortage of background
information for this issue.
> Greetings from Finland
Send me some vodka. Thanks in advance.
.B ekiM
http://www.nwlink.com/~mikeblas/
----------
From: owner-mfc-l@netcom.com on behalf of Pertti Anttila
Sent: Sunday, July 21, 1996 2:55 AM
To: mfc-l@netcom.com
Subject: Problem with CList Serializion
I have a big trouble with CList Serializion!
============================================
I've made a program that holds most of it's data in a CList object,
but now I can't save that data to disk with serializion properly.
So I made a little test program just for testing. I made two list:
CList int_list;
CList string_list;
void CMySerialDoc::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
int_list.Serialize(ar);
string_list.Serialize(ar);
}
These lists works just fine. I can use them as I expect, no
problems with serializion.
Next I made a class containing an integer and a string. And
a list containing objects of that type.
CList class_list;
void CMySerialDoc::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
int_list.Serialize(ar);
string_list.Serialize(ar);
class_list.Serialize(ar);
}
When I build my application, every thing works just fine: no errors,
no warnings. On the window every list works just fine. When I save
the document to disk, still okey. But when I start the program again,
everything falls down.
The program gives an exception:
--- Error message: ---
Assertion Failed!
MySerial: File afxmem.cpp Line 367
So there is something wrong with my class! But I don't know what.
Please, could anyone tell me, how to make the next class so, that I
can serialize it's objects to disk file with CList object:
// DECLARATION
class CTesti : public CObject // declaration, inherited from CObject
{
public: // attributes
CString nimi;
int numero;
public:
CTesti() {} // default constructor, empty
virtual ~CTesti() {}; // virtual destructor, empty
// ( no new/delete )
void Serialize(CArchive & ar); // override Serialize()
CTesti& operator=(CTesti &joku);// needed in
//class_list->AddTail()
DECLARE_SERIAL(CTesti); // I thougth this is all you need...
};
// IMPLEMENTATION
IMPLEMENT_SERIAL(CTesti, CObject, 0); // At least allmost all...
CTesti& CTesti::operator=(CTesti &joku)
{
nimi = joku.nimi;
numero = joku.numero;
return *this;
}
void CTesti::Serialize(CArchive & ar)
{
if (ar.IsStoring())
{
ar << nimi;
ar << (WORD)numero;
}
else
{
WORD apu;
ar >> nimi;
ar >> apu;
numero = apu;
}
}
Environment: VC++ 2.0 / Win95
Greetings from Finland
Pertti Anttila
| Вернуться в корень Архива
|