序列化不适用于 MFC 中的 CMapWordToOb 类

问题描述 投票:0回答:1

我正在尝试在 MFC 中测试序列化,目前我陷入了此类 CMapWordToOb。我尝试了一个单元测试用例来序列化然后反序列化映射中传递的数据,但在 Visual Studio 上编译后得到中止窗口MFC 作为共享 DLL。有没有人尝试过 CMapWordtoOb 的序列化,如果它对他们有用? 我正在尝试测试 void CMapWordToOb::Serialize(CArchive& ar)

#include <stdafx.h>
#include <afxtempl.h> 
#include <afx.h>     
#include <iostream>

class CAge : public CObject {
    DECLARE_SERIAL(CAge)
public:
    CAge() {}
    CAge(int age) {
        m_age = age;
    }
    int getAge() const {
        return m_age;
    }
private:
    int m_age;
};

bool CompareMaps(const CMapWordToOb& map1, const CMapWordToOb& map2) {
    // Check if the maps have the same count of elements
    if (map1.GetCount() != map2.GetCount()) {
        return false;
    }

// Iterate through all elements in the first map
POSITION pos = map1.GetStartPosition();
CAge *pa1, *pa2;
WORD key1, key2;
for (pos = map1.GetStartPosition(); pos != NULL;) {
    map1.GetNextAssoc(pos, key1, (CObject *&)pa1);

    // Check if the key exists in the second map
    if (!map2.Lookup(key1, (CObject *&)pa2)) {
        return false;
    }

    // Check if the values are the same
    if (pa1->getAge() != pa2->getAge()) {
        return false;
    }
}

// Maps are equal
return true;
}


// Function to print the contents of a CMapWordToOb object
void PrintMap(const CMapWordToOb& map) {
    std::cout << "MapWordtoOb: ";
    POSITION pos = map.GetStartPosition();
    CAge *pa;
    WORD key;
    for (pos = map.GetStartPosition(); pos != NULL;)
    {
        map.GetNextAssoc(pos, key, (CObject *&)pa);
        std::cout << key << " : " << pa->getAge() << std::endl;
    }
}


// Test case for serializing and deserializing CMapWordtoOb
void TestSerializeCMapWordtoOb() {
    std::cout << "..........Start Of Test Cases for CMapWordtoOb Serialization.........." << std::endl;
    CMapWordToOb map;
    map.SetAt(3, new CAge(20));
    map.SetAt(4, new CAge(30));
    map.SetAt(2, new CAge(40));

std::cout << "CMap created" << std::endl;
// Serialize the map
CFile file(_T("mapData.dat"), CFile::modeCreate | CFile::modeWrite);
CArchive arStore(&file, CArchive::store);
map.Serialize(arStore); //crashing here
arStore.Close();
file.Close();

// Deserialize the map from file
CMapWordToOb deserializedMap;
CFile deserializedFile(_T("mapData.dat"), CFile::modeRead);
CArchive arLoad(&deserializedFile, CArchive::load);
deserializedMap.Serialize(arLoad);
arLoad.Close();
deserializedFile.Close();

std::cout << "Deserialized ";
PrintMap(deserializedMap);

// Compare the original map with the deserialized one
if (CompareMaps(deserializedMap, map)) {
    std::cout << "Unit test passed: Serialization and deserialization successful." << std::endl;
}
else {
    std::cout << "Unit test failed: Serialization and deserialization failed." << std::endl;
}

std::cout << "..........End Of Test Cases for CMapWordtoOb Serialization.........." << std::endl;
}

IMPLEMENT_SERIAL(CAge, CObject, 1);
int main() {
    TestSerializeCMapWordtoOb();
    return 0;
}
c++ unit-testing serialization mfc
1个回答
0
投票

您忘记在

Serialize
类中实现
CAge
方法。

将此添加到您的

CAge
类定义中:

void Serialize(CArchive& ar);

并添加此实现:

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

    if (ar.IsStoring())
        ar << m_age;
    else
        ar >> m_age;
}
© www.soinside.com 2019 - 2024. All rights reserved.