C++ cereal库使用记录

C++ cereal库使用记录

以下代码报错,报错原因是cereal不支持原生指针

{
        auto screamInfo1 = new ConfigInfo::ScreamInfo();
        ConfigInfo::ScreamInfo* screamInfo2 = new ConfigInfo::ScreamInfo();
        ConfigInfo::ScreamInfo& screamInfo3 = *screamInfo2;
        std::ofstream file("screamInfo.json");
        cereal::JSONOutputArchive archive(file);
        // 保存时的名字为“*screamInfo1”,如果需要名字中不带*号可以使用引用包裹起来。
        // cereal不支持原生指针
        archive(CEREAL_NVP(screamInfo1), CEREAL_NVP(screamInfo2));
        archive(CEREAL_NVP(screamInfo3));
    }
    {
        auto screamInfo1 = new ConfigInfo::ScreamInfo();
        ConfigInfo::ScreamInfo* screamInfo2 = new ConfigInfo::ScreamInfo();
        ConfigInfo::ScreamInfo& screamInfo3 = *screamInfo2;
        memset(screamInfo1, 0, sizeof(ConfigInfo::ScreamInfo));
        memset(screamInfo2, 0, sizeof(ConfigInfo::ScreamInfo));
        memset(&screamInfo3, 0, sizeof(ConfigInfo::ScreamInfo));
        std::ifstream file_in("screamInfo.json");
        cereal::JSONInputArchive archive(file_in);
        // 加载
        archive(CEREAL_NVP(screamInfo1), CEREAL_NVP(screamInfo2));
        archive(CEREAL_NVP(screamInfo3));
        file_in.close();
    }
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp:114:67: note: 'std::integral_constant<bool, false>::value' evaluates to false
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp: In instantiation of 'void cereal::serialize(Archive&, T*&) [with Archive = cereal::JSONOutputArchive; T = ConfigInfo::ScreamInfo]':
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:516:39:   required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = ConfigInfo::ScreamInfo*; 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::sfinae)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:444:26:   required from 'void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = ConfigInfo::ScreamInfo*&; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:333:22:   required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {ConfigInfo::ScreamInfo*&}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/archives/JSON.hpp:966:7:   required from 'void cereal::save(cereal::JSONOutputArchive&, const cereal::NameValuePair<T>&) [with T = ConfigInfo::ScreamInfo*&]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:532:34:   required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = cereal::NameValuePair<ConfigInfo::ScreamInfo*&>; typename cereal::traits::detail::EnableIfHelper<cereal::traits::has_non_member_save<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_save<T, ArchiveType>::value || (! cereal::traits::is_specialized<T, ArchiveType>::value)))>::type <anonymous> = (cereal::traits::detail::sfinae)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:444:26:   required from 'void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = cereal::NameValuePair<ConfigInfo::ScreamInfo*&>; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:452:22:   required from 'void cereal::OutputArchive<ArchiveType, Flags>::process(T&&, Other&& ...) [with T = cereal::NameValuePair<ConfigInfo::ScreamInfo*&>; Other = {cereal::NameValuePair<ConfigInfo::ScreamInfo*&>}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:333:22:   required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {cereal::NameValuePair<ConfigInfo::ScreamInfo*&>, cereal::NameValuePair<ConfigInfo::ScreamInfo*&>}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:\apps\IR_Touch\mainwindow.cpp:49:16:   required from here
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp:114:67: error: static assertion failed: Cereal does not support serializing raw pointers - please use a smart pointer
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp:114:67: note: 'std::integral_constant<bool, false>::value' evaluates to false
[build] mingw32-make.exe[2]: *** [CMakeFiles\IR_Touch.dir\build.make:113: CMakeFiles/IR_Touch.dir/mainwindow.cpp.obj] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:85: CMakeFiles/IR_Touch.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:135: all] Error 2

此外,调用序列化的方法中的CEREAL_NVP宏由于直接讲变量明转为字符串:#T,所以在保存时的名字为“*screamInfo”,如果需要导出的Json中变量名中不带*号,可以使用引用包裹起来。

  #define CEREAL_NVP(T) ::cereal::make_nvp(#T, T)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值