【问题标题】:Serializing a CTypedPtrList in MFC Does Not Save the Data in the List在 MFC 中序列化 CTypedPtrList 不会将数据保存在列表中
【发布时间】:2016-02-14 21:46:39
【问题描述】:

当我尝试序列化 CTypedPtrList 然后从文件中检索该列表时,会保存列表中的对象数,但不会保存对象中的实际数据。

例如,如果我要保存到文件然后从文件加载,我可以循环浏览所有四个条目,但没有一个变量会包含它们的数据。

这是适用的代码。

LEDFrame.h:

#pragma once
#include "afx.h"

#define NUMLAYERS 12
#define NUMROWS 8
#define NUMCOLUMNS 12

class LEDFrame :
    public CObject
{
    DECLARE_SERIAL(LEDFrame)  // DECLARE_SERIAL macro call to enable serialization of CObjects
public:
    bool data[NUMLAYERS][NUMROWS][NUMCOLUMNS]; // this holds one frame of the animation
    int m_FrameOnTime; // this holds how long the frame is on for, a value of 1 is 10 ms
    LEDFrame();
    ~LEDFrame();
};

typedef CTypedPtrList<CObList, LEDFrame*> CLEDFrameList; // this is the list of frames, CTypedPtrlList can be shared between document and view

LEDFrame.cpp

#include "stdafx.h"
#include "LEDFrame.h"

IMPLEMENT_SERIAL(LEDFrame, CObject, 0) // implement_serial macro call for serialization

LEDFrame::LEDFrame()
{
}

LEDFrame::~LEDFrame()
{
}

LEDCubeDoc.h

public:
    virtual void Serialize(CArchive& ar);
private:
    CLEDFrameList myLEDFrameList;

LEDCubeDoc.cpp

void CLEDCubeDoc::Serialize(CArchive& ar)
{
    TRACE("Entering CLEDCubeDoc::Serialize");
    if (ar.IsStoring())
    {
        // TODO: add storing code here
    }
    else
    {
        // TODO: add loading code here
    }
    myLEDFrameList.Serialize(ar);
}

LEDCubeView.h

public:
    CLEDFrameList* myViewLEDFrameList;  // a pointer to the documents myLEDFrameList, this edits myLEDFrameList in the doc

我认为它可能无法将列表正确保存到磁盘。这是因为当我查看保存的文件时,它只有 22 个字节。

我在 stdafx.h 中包含“afxtempl”。

我的程序基于 www.tenouk.com 上的 MFC 教程的模块 10 和 11。网址是http://www.tenouk.com/cplusplusnmfc.html

您能帮我纠正这个问题,以便我可以序列化我的 CTypedPtrList 吗?

谢谢,

安德鲁

【问题讨论】:

    标签: c++ mfc visual-studio-2015


    【解决方案1】:

    我看不到任何LEDFrame::Serialize 的声明,所以我猜你还没有实现它。这意味着,CObject 的序列化方法将被调用,并且该函数应该如何知道,你的类里面有什么?!

    顺便说一句:在ar.IsStoring 的两种情况下,您都必须调用myLEDFrameList.Serialize(ar); - 一种用于存储,其他部分用于加载。如果您需要特殊的加载顺序或必须调用某些函数来帮助加载,您可以在此处进行。

    【讨论】:

    • 我想添加 LEDFrame::Serialize,但它会序列化什么? myLEDFrameList 是 LEDCubeDoc 的一部分,而不是 LEDFrame。我认为 LEDFrame::Serialize() 会因为以下语句而被调用: typedef CTypedPtrList
    猜你喜欢
    • 1970-01-01
    • 2012-07-28
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多