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

问题描述

我有3个自定义对象:
-包含文件信息的文件对象
-包含文件对象的组对象
-包含组对象的库对象

文件对象包含字符串,整数,字节和布尔值.

组对象具有文件对象的CObArray以及包含组名的字符串和2个字节,其中包含有关组的信息.

该库对象包含一个组对象的CObArray.

我序列化所有对象并将其保存到文件中.但是,当我打开信息并将其序列化回对象时,组对象名称和字节会与错误的文件类型对象列表相关联.

这是文件类型对象序列化代码:

I have 3 custom made objects:
-An file object that contains file information
-A group object that contains file objects
-A library object which contains group objects

The file object contains strings, integers, bytes, and bools.

The group object has a CObArray of file objects along with a string, containing the group name, and 2 bytes that contain information about the group.

The library object contains a CObArray of group objects.

I serialize all of the objects and save them to a file. However, when I open and serialize the information back into the objects, the group object name and bytes get associated with the wrong list of file type objects.

Here''s the file type object serialization code:

void CMyFile::Serialize(CArchive &ar)
{
	CObject::Serialize(ar);

	if(ar.IsStoring())
	{
		ar << (char)bType << (BYTE)btStyle << (CString)...;
	}
	else
	{
		ar >> (char&)bType >> (BYTE)btStyle >> (CString&)...;
	}
}


这是组对象序列化代码:


here''s the group object serialization code:

void CGroup::Serialize(CArchive &ar)
{
	CObject::Serialize(ar);
	if(ar.IsStoring())
	{
	     ar << (BYTE)btGrpStyle << (BYTE)btCategory << (CString)...;
	}
	else
	{
	     ar >> (BYTE&)btGrpStyle >> (BYTE&)btCategory >> (CString&)...;
	}
	oaTypeFiles.Serialize(ar);
}


这是库的序列化代码:


Here''s the library serialize code:

void CLibrary::Serialize(CArchive &ar)
{
    CObject::Serialize(ar);

    oaGroups.Serialize(ar);
}



当我保存并拥有更多的一组时,我得到以下信息:

一个名为儿童书籍"的小组文件有2个文件:Mary Poppins和Curious George.

一个名为"Fantasy Books"的群组文件有1个文件:Harry Potter

我保存了文件.

当我打开文件时,会发生以下情况:

一个名为"Fantasy Books"的小组有2个文件:Mary Poppins和Curious George.

名为儿童书籍"的小组文件有1个文件:哈利·波特

几乎就像是将非ObjectArray数据反向序列化一样.

任何帮助或技巧将不胜感激.



When I save and have more the one group I get the following:

A group file called ''Children''s Books'' has 2 files: Mary Poppins and Curious George.

A group file called ''Fantasy Books'' has 1 file: Harry Potter

I save the file.

When I open the file the following happens:

A group called ''Fantasy Books'' has 2 files:Mary Poppins and Curious George.

A group file called ''Children''s Books'' has 1 file: Harry Potter

It''s almost like it serializes the non ObjectArray data in reverse.

Any help or tips will be greatly appreciated.

推荐答案



void CGroup::Serialize(CArchive &ar)
{
  ...
  TRACE(_T("[%s of % group]: "),
        ar.IsStoring() ? _T("Writing") : _T("Reading"),
        m_cszName /*group name member*/);
  oaTypeFiles.Serialize(ar);
  TRACE(_T("\n"));
}

void CMyFile::Serialize(CArchive &ar)
{
  ...
  TRACE(_T("%s; "), m_cszName /*file name member*/);
}


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

08-22 17:07