首页
社区
课程
招聘
[求助]vc 数据序列化报错,请高人指点。
发表于: 2013-5-31 21:14 3267

[求助]vc 数据序列化报错,请高人指点。

2013-5-31 21:14
3267
我用vc6写了一个数据序列化的程序,但是运行报错,请高人指点。
附件包含完整工程文件。

#if !defined(AFX_TESTARRAY_H__D82D4BD4_F475_453A_A68D_99DF0A5D96B3__INCLUDED_)
#define AFX_TESTARRAY_H__D82D4BD4_F475_453A_A68D_99DF0A5D96B3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

typedef struct  
{
	
	double number1;
	double number2;
	double iarray[3];
	CString name;
}spt;

class CTestArray : public CObject 

{

public:
	CTestArray(CTestArray &CMyTestArray);

	DECLARE_SERIAL(CTestArray)
	void PrintNumber();
	CTestArray(double i,double j);
virtual	void Serialize( CArchive& archive );

	CTestArray();
	virtual ~CTestArray();
	spt Pointspt;
	CTestArray& CTestArray::operator = (CTestArray &CMyTestArray);
};

#endif // !defined(AFX_TESTARRAY_H__D82D4BD4_F475_453A_A68D_99DF0A5D96B3__INCLUDED_)


/ TestArray.cpp: implementation of the CTestArray class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TestArray.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CTestArray,CObject,0)

CTestArray::CTestArray()
{

}

CTestArray::~CTestArray()
{

}

void CTestArray::Serialize(CArchive &archive)
{
// 	CObject:Serialize(archive);
	if (archive.IsStoring())// 非零就是存储
	{
		archive << Pointspt.number1;
		archive << Pointspt.number2;
		archive << Pointspt.iarray[0]<<Pointspt.iarray[1]<<Pointspt.iarray[2];
		archive << Pointspt.name;
	} 
	else
	{
		archive>> Pointspt.number1;
		archive>> Pointspt.number2;
		archive >> Pointspt.iarray[0]>>Pointspt.iarray[1]>>Pointspt.iarray[2];
		archive>> Pointspt.name;
	}
}

CTestArray::CTestArray(double i, double j)
{
	this->Pointspt.number1 =i;
	this->Pointspt.number2=j;
	this->Pointspt.iarray[0] = i;
	this->Pointspt.iarray[1] =j;
	this->Pointspt.iarray[2] =i+j;
	this->Pointspt.name="hhhhh";

}

//DEL void CTestArray::SetNumber(int i, int j)
//DEL {
//DEL 	this->number1 =i;
//DEL 	this->number2 =j;
//DEL }

void CTestArray::PrintNumber()
{
	printf("number1 = %f ,number2 = %f\n",this->Pointspt.number1,this->Pointspt.number2);
	printf("iarry0 = %f, iary1 = %f ,iarry2 = %f\n",this->Pointspt.iarray[0],this->Pointspt.iarray[1],this->Pointspt.iarray[2]);
}

CTestArray& CTestArray::operator = (CTestArray &CMyTestArray)
 {
		this->Pointspt.number2 = CMyTestArray.Pointspt.number2;
	this->Pointspt.number1= CMyTestArray.Pointspt.number1;
	this->Pointspt.iarray[0] = CMyTestArray.Pointspt.iarray[0];
	this->Pointspt.iarray[1] =CMyTestArray.Pointspt.iarray[1];
	this->Pointspt.iarray[2] = CMyTestArray.Pointspt.iarray[2];
	this->Pointspt.name= CMyTestArray.Pointspt.name;
	
	return *this;
}


CTestArray::CTestArray(CTestArray &CMyTestArray)
{
	this->Pointspt.number2 = CMyTestArray.Pointspt.number2;
	this->Pointspt.number1= CMyTestArray.Pointspt.number1;
	this->Pointspt.iarray[0] = CMyTestArray.Pointspt.iarray[0];
	this->Pointspt.iarray[1] =CMyTestArray.Pointspt.iarray[1];
	this->Pointspt.iarray[2] = CMyTestArray.Pointspt.iarray[2];
	this->Pointspt.name= CMyTestArray.Pointspt.name;


}


// TestSerialize.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <afxtempl.h>

void Wridefile()
{
	printf("save Carry object to file\n");
	CArray<CTestArray,CTestArray&> CmyArray;
	CTestArray ctest(2,3);
	CTestArray ctest2(4,5);
	CmyArray.Add(ctest);
	ctest.Pointspt.number1 = 0;
	ctest.Pointspt.number2 =999;
	CmyArray.Add(ctest);
	CmyArray.Add(ctest2);
	CmyArray.Add(ctest);
	ctest2.Pointspt.name="fuck";
	CmyArray.Add(ctest2);
	
	CFile cmyfile;
	cmyfile.Open("kk.bak",CFile.modeCreate|CFile.modeWrite);
	CArchive arStore(&cmyfile, CArchive::store);
	CmyArray.Serialize(arStore);
	arStore.Close();
	cmyfile.Close();
	
}

void readfile()
{
	printf("Read  object  from file\n");

	CArray<CTestArray,CTestArray&> CmyArray;
	CFile cmyfile;
	cmyfile.Open("kk.bak",CFile.modeRead);
	CArchive arload(&cmyfile,CArchive::load);
	CmyArray.Serialize(arload);
	printf("the number of array = %d\n",CmyArray.GetSize());
	CTestArray tempCteatarray;
	for (int i=0;i<CmyArray.GetSize();i++)
	{
		tempCteatarray = CmyArray.GetAt(i);
		printf("number1 = %f, number2=%f, iarray[0] =%f, name = %s\n",
			tempCteatarray.Pointspt.number1
			,tempCteatarray.Pointspt.number2
			,tempCteatarray.Pointspt.iarray[0]
			,tempCteatarray.Pointspt.name);
	}
}
int main(int argc, char* argv[])
{
	Wridefile();
	readfile();

	return 0;
}


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 6
活跃值: (133)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
哪位兄弟熟悉,帮我看下,确实不知是哪里的问题
我是初学,哪里有错误,告诉我下吧,

附件里面有完整的工程文件。
2013-5-31 21:18
0
雪    币: 6
活跃值: (133)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
刚才调了下,去掉CString那个的name,就不报错了

难道CString 的特殊处理么
2013-5-31 21:25
0
游客
登录 | 注册 方可回帖
返回
//