-
Serializing a class
I'm very frustrated at this right now. I can't seem to figure it out.
I created my own class called "CMais" and it used a generic class and the base class of the CObjectArray. I added 10 member variables and am trying to now serialize the class. When I go to add the member function, I type the function as "Serialize(CArchive &ar)" and the access as public and the virtual box is checked. When I click the "OK" button, it comes up with an error that "Serialize(CArchive &ar) is not a valid function in C++":o. What does this mean and how can I fix it?
Thanks for all your help. :confused:
I have attached a copy of my Mais.cpp file for you to look at for a deeper involved look.
-
To "serialize" a class, you would normally overload the stream insertion/extraction operators, (<< and >>), search google, or this board.
-
serialization
to implement serialization your object must be derive from CObject
[CODE]
//in h file
class CMyclass : CObject
{
DECLARE_SERIAL(CMyclass)
...
void Serialize( CArchive& ar);
int m_1;
int m_2;
}
/////////////////////////////////////////////
//in cpp file
IMPLEMENT_SERIAL( CMyclass, CObject, 0)
void CMyclass::Serialize( CArchive& ar )
{
if( ar.IsStoring( ar ) )
ar << m_1 << m_2 ;
}
/////in the foreing class
class SomeForeignClass{
CMyclass a;
CFile myArFile;
}
void SomeForeignClass::OnLoad()
{
if( myArFile.Open(strFileName, CFile::modeRead))
{
CArchive ar(&myArFile, CArchive::load);
a.Serialize( ar )l
myArFile.Close();
}
}
void SomeForeignClass::OnSave()
{
if( myArFile.Open( strFileName, CFile::modeCreate | CFile::modeWrite))
{
CArchive ar(&myArFile, CArchive::store)
a.Serialize( ar );
ar.Close();
myArFile.Close();
}
}
[CODE]
this is somewhat of a sample similar to the one found in "Introduction to MFC programming with Visual C++" by Richard M. Jones....
-
I have a similar question.. what if your class is Cwnd which is derived from CObject?
because i'm doing what you just showed on top.. but with Cwnd.. and well it ain't working..
-
nevermind, i got it.. apprently you have to have a default constructor..