类序列化问题 [英] class Serialization problem

查看:81
本文介绍了类序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在课上碰壁.
我的班级如下所示

//.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;
}


现在为了支持序列化,我更改了代码,如下所示

//.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;
}


在下面的函数中,我将Test类对象存储到文件中

void CSerailizationDlg::OnStoreButton() 
{	
	Test TestObj;
	TestObj.SetData( 1, "Testing" );
	CFile file;
	file.Open( "test", CFile::modeReadWrite );
	CArchive arch( &file, CArchive::store );
	arch << TestObj;
	:
	:
}

这有什么问题或遗漏了什么吗?
我收到错误二进制"<<:没有定义运算符,该运算符使用类型为"class CArchive"的左侧操作数"
我认为没有必要超载<<我的Test类中的运算符,因为宏是这样做的.
这里出了什么问题?
任何人都可以提供帮助..plsss

解决方案

CArchive提供了<<和>>用于在文件中写入和读取简单数据类型以及CObject的运算符.

CArchive的功能原型如下所示:
朋友CArchive& AFXAPI运算符<<(CArchive& ar,const CObject * pOb);

序列化和反序列化的示例代码如下所示

//序列化
测试TestObj;
TestObj.SetData(23,"Testing");
CFile文件;
file.Open("C:\\ test",CFile :: modeReadWrite);
CArchive arch(& file,CArchive :: store);
arch<<& TestObj;


//反序列化
测试* pTestObj1;
CFile file1;
file1.Open("C:\\ test",CFile :: modeReadWrite);
CArchive arch1(& file1,CArchive :: load);
我认为问题在于CArchive并未定义采用类型为"Test"的对象的二进制<<"运算符,除非测试"源自"CObject".这就是为什么arch << TestObj调用无法编译的原因.如果将其替换为TestObj.Serialize(arch)或从"CObject"继承"Test",它将进行编译(我认为您可能需要将指针传递给CObject派生的类,因此调用看起来像arch << &TestObj ).


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

解决方案

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;


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).


这篇关于类序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆