gpt4 book ai didi

c++ - 使用多态类 boost XML归档编译错误

转载 作者:行者123 更新时间:2023-12-02 10:09:20 25 4
gpt4 key购买 nike

我正在尝试使用增强归档将文件保存为XML格式。
我在编译时遇到了这两个错误。

  • 错误C2664'int boost::mpl::assertion_failed(boost::mpl::assert::type)':无法从'boost::mpl::failed ***********转换参数1将boost::serialization::is_wrapper::***********'提升为'boost::mpl::assert::type'
  • 错误C2789'mpl_assertion_in_line_6':必须初始化const限定类型的对象

  • 由于这两行而发生这种情况。
    BOOST_CLASS_EXPORT_GUID(SumKeyframeXYZ, "SumKeyframeXYZ")
    BOOST_CLASS_EXPORT_GUID(SumPosition_Channel, "SumPosition_Channel")
    这是主要功能。
    #include "Container.h"

    BOOST_CLASS_EXPORT_GUID(SumKeyframeXYZ, "SumKeyframeXYZ")
    BOOST_CLASS_EXPORT_GUID(SumPosition_Channel, "SumPosition_Channel")

    int main()
    {
    std::ofstream ofs("D:\\output.xml");
    Container cont;
    cont.AddPositionChannel();
    boost::archive::xml_oarchive oa(ofs);
    oa << BOOST_SERIALIZATION_NVP(cont);
    return 0;
    }
    这就是类结构的样子。
    容器.h
    #include <string>
    #include "SumChannel.h"
    #include "SumPosition_Channel.h"
    #include <fstream>
    #include <iostream>
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/vector.hpp>
    #include <boost/serialization/shared_ptr.hpp>

    class Container
    {
    private:
    std::string stdstrName;
    std::vector<std::shared_ptr<SumChannel>> Channels;

    public:
    Container() { stdstrName = "Container Name"; }
    void AddPositionChannel() {
    std::shared_ptr< SumPosition_Channel> posChannel = std::make_shared<SumPosition_Channel>();
    Channels.push_back(posChannel);
    };


    private:

    friend class boost::serialization::access;
    template<class Archive> void serialize(Archive & ar,
    const unsigned int version) const {
    ar & BOOST_SERIALIZATION_NVP(stdstrName);
    ar & BOOST_SERIALIZATION_NVP(Channels);
    }

    };
    //////////////////////////////////////////////////// ////////
    SumChannel.h
    #include <fstream>
    #include <iostream>
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/vector.hpp>

    class SumChannel
    {
    public:
    float offset;

    SumChannel() { offset = 1.0; }

    private:

    friend class boost::serialization::access;

    template<class Archive> void serialize(Archive & ar,
    const unsigned int version) const {
    ar & BOOST_SERIALIZATION_NVP(offset);
    }
    };
    SumPosition_Channel
    #include "SumKeyframeXYZ.h"
    #include "SumChannel.h"
    #include <vector>
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/vector.hpp>

    class SumPosition_Channel : public SumChannel
    {
    public:
    std::vector<SumKeyframeXYZ> keyframes;

    void AddKeyframe() { SumKeyframeXYZ key; keyframes.push_back(key); }

    private:
    friend class boost::serialization::access;

    typedef SumChannel _Super;
    template<class Archive> void serialize(Archive & ar,
    const unsigned int version) const {
    ar & boost::serialization::base_object<_Super>(*this);
    ar & BOOST_SERIALIZATION_NVP(keyframes);
    }
    };
    KeyFrameBase.h
    #include <iostream>
    #include <fstream>

    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/archive/xml_iarchive.hpp>
    class Keyframebase
    {
    public:
    std::string stdstrName;
    float time;

    Keyframebase() { stdstrName = "BaseKey"; time = 50.0; }

    private:

    friend class boost::serialization::access;

    template<class Archive> void serialize(Archive & ar,
    const unsigned int version) {
    ar & BOOST_SERIALIZATION_NVP(stdstrName);
    ar & BOOST_SERIALIZATION_NVP(time);
    }

    };
    SumKeyframeXYZ.h
    #include "KeyframeBase.h"
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/vector.hpp>

    class SumKeyframeXYZ : public Keyframebase
    {
    public:
    float x;
    float y;
    float z;

    SumKeyframeXYZ() { x = 150.0; y = 200.0; z = 300.0; }

    private:
    friend class boost::serialization::access;

    template<class Archive> void serialize(Archive & ar,
    const unsigned int version) const {
    ar & boost::serialization::base_object<Keyframebase>(*this);
    ar & BOOST_SERIALIZATION_NVP(x);
    ar & BOOST_SERIALIZATION_NVP(y);
    ar & BOOST_SERIALIZATION_NVP(z);
    }
    };

    最佳答案

    代码的问题是我没有将NVP写入基类
    我从更改了基类归档代码

      ar & boost::serialization::base_object<_Super>(*this);
    对于所有基类而言。
    typedef SumChannel _Super;
    ar & boost::serialization::make_nvp("SumChannel", boost::serialization::base_object<_Super>(*this));
    并从serilize函数中删除了const。

    关于c++ - 使用多态类 boost XML归档编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64262836/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com