使用Cereal的C ++序列化错误:Cereal不支持序列化原始指针

时间:2018-02-28 23:39:42

标签: c++ serialization cereal

我正在使用谷歌 C ++序列化库(https://uscilab.github.io/cereal/index.html)来序列化一些数据。

我尝试序列化的数据类型主要包括向量的向量(例如:vector<vector<ushort>>)和基本类型(例如:uint, vector<string>)。

编译时,我收到以下错误:

src/utils/cereal/types/common.hpp: In instantiation of ‘void cereal::serialize(Archive&, T*&) [with Archive = cereal::BinaryOutputArchive; T = Graph]’:
src/utils/cereal/cereal.hpp:408:39:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = Graph*; typename cereal::traits::detail::EnableIfHelper<cereal::traits::has_non_member_serialize<T, ArchiveType>::value, (! cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value), (cereal::traits::is_output_serializable<T, ArchiveType>::value && (cereal::traits::is_specialized_non_member_serialize<T, ArchiveType>::value || (! cereal::traits::is_specialized<T, ArchiveType>::value)))>::type <anonymous> = (cereal::traits::detail::type)0; ArchiveType = cereal::BinaryOutputArchive; unsigned int Flags = 1u]’
src/utils/cereal/cereal.hpp:347:9:   required from ‘void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = Graph*; ArchiveType = cereal::BinaryOutputArchive; unsigned int Flags = 1u]’
src/utils/cereal/cereal.hpp:249:9:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {Graph*}; ArchiveType = cereal::BinaryOutputArchive; unsigned int Flags = 1u]’
src/Graph.cpp:169:22:   required from here
src/utils/cereal/types/common.hpp:114:5: error: static assertion failed: Cereal does not support serializing raw pointers - please use a smart pointer
     static_assert(cereal::traits::detail::delay_static_assert<T>::value,

Google搜索引导我访问以下有关错误的页面:https://uscilab.github.io/cereal/pointers.html

此页面表明错误的原因是序列化“哑”指针,或序列化没有默认构造函数的类型。

但是,我序列化的Graph类有一个默认构造函数Graph(),我不认为我正在序列化任何“哑”指针。

相关的Graph班级代码:

    Graph();
    vector<vector<ushort>> adjacencyList();
    vector<string> name();
    unordered_map<string, ushort> unorderedNodeNameMap();
    uint size;

    friend class cereal::access;

    template<class Archive>
    void serialize(Archive& archive)
    {
        archive(CEREAL_NVP(adjacencyList), CEREAL_NVP(name), CEREAL_NVP(unorderedNodeNameMap), CEREAL_NVP(size);
    }

序列化:

void Graph::serializeGraph()
{
    ofstream ofs("b.out", ofstream::binary);
    {
        cereal::BinaryOutputArchive oArchive(ofs);
        oArchive(this);
    }
}

1 个答案:

答案 0 :(得分:0)

我忘了this是指针类型。我想除了不能直接序列化“哑”指针外,你也无法将它们传递给输出档案。

将序列化对象作为值传递解决了它:

void Graph::serializeGraph(Graph a)
{
    ofstream ofs("b.out", ofstream::binary);
    {
        cereal::BinaryOutputArchive oArchive(ofs);
        oArchive(a);
    }
}