Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i need to serailize my class.
my class looks like below
//.h
class Test
{
	int m_nData;
	CString m_csData;
public:
	void SetData( int nData_i, CString csData_i );
	void GetData( int& nData_o, CString& csData_o );
};

//.cpp
void Test::SetData( int nData_i, CString csData_i )
{
	m_nData = nData_i;
	m_csData = csData_i;
}
void Test::GetData( int& nData_o, CString& csData_o )
{
	nData_o = m_nData;
	csData_o = m_csData;
}

now to support serailization i changed the code as shown below
//.h
class Test : public CObject
{
	DECLARE_SERIAL (Test)
	int m_nData;
	CString m_csData;
public:
	Test()
	{
	}
	void SetData( int nData_i, CString csData_i );
	void GetData( int& nData_o, CString& csData_o );
	void Serialize (CArchive& ar);
};

//.cpp
IMPLEMENT_SERIAL (Test, CObject, 1)
void Test::SetData( int nData_i, CString csData_i )
{
	m_nData = nData_i;
	m_csData = csData_i;
}
void Test::GetData( int& nData_o, CString& csData_o )
{
	nData_o = m_nData;
	csData_o = m_csData;
}
void Test::Serialize (CArchive& ar)
{
    CObject::Serialize (ar);
    if (ar.IsStoring ())
        ar << m_nData << m_csData;
    else // Loading, not storing
        ar >> m_nData >> m_csData;
}

and in below function i am storing the Test class object to a file
void CSerailizationDlg::OnStoreButton() 
{	
	Test TestObj;
	TestObj.SetData( 1, "Testing" );
	CFile file;
	file.Open( "test", CFile::modeReadWrite );
	CArchive arch( &file, CArchive::store );
	arch << TestObj;
	:
	:
}
Is there anything wrong with this or missing anything?
i get the error "binary '<<' : no operator defined which takes a left-hand operand of type 'class CArchive'"
I think there is no need to overload << operator in my Test class since macros do it.
What is going wrong here?
can anyone help..plsss
Posted
Updated 8-Jun-11 19:39pm
v2

CArchive provides << and >> operators for writing and reading simple data types as well as CObjects to and from a file.

The function prototypes of CArchive is shown below:
friend CArchive& AFXAPI operator<<(CArchive& ar, const CObject* pOb);

The sample code to serialize and deserialize are shown below

// Serialize
Test TestObj;
TestObj.SetData( 23, "Testing" );
CFile file;
file.Open( "C:\\test", CFile::modeReadWrite );
CArchive arch( &file, CArchive::store );
arch<<&TestObj;


// Deserialize
Test* pTestObj1;
CFile file1;
file1.Open( "C:\\test", CFile::modeReadWrite );
CArchive arch1( &file1, CArchive::load );
arch1>>pTestObj1;
 
Share this answer
 
I think the problem is that CArchive does not define a binary '<<' operator that takes objects of type 'Test', unless 'Test' derives from 'CObject'. That's why arch << TestObj call does not compile. If you replace it with TestObj.Serialize(arch) or inherit 'Test' from 'CObject', it will compile (I think you might need to pass a pointer to CObject - derived classes, so the call would look like arch << &TestObj).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900