在我的设置中,我将OpacityChannel指针的 vector 作为Container类的成员。

如果我创建一个OpacityChannel变量并将其写入存档,那么一切都会按预期进行。

当我编写Container类对象并将其写入存档时,它错过了作为OpacityChannel类成员的KeyframeFloat类的数据。

如果我从ar&cont;更改行,这是我的主文件。到ar&opacityChannel,然后根据需要写入数据。

我无法理解为什么在编写Container类时会丢失数据。

#include "pch.h"
#include <iostream>
#include<fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "Container.h"
#include "KeyframeFloat.h"
#include <boost/serialization/export.hpp>


BOOST_CLASS_EXPORT_GUID(SumOpacity_Channel, "SumOpacity_Channel")
BOOST_CLASS_EXPORT_GUID(SumKeyframeFloat, "SumKeyframeFloat")

int main()
{
    const char* fileName = "saved.txt";
    std::vector<int> vec;
    Container cont;
    SumOpacity_Channel opacityChannel;
    SumKeyframeFloat key1, key2;
    opacityChannel.AddKeyframe(key1);
    opacityChannel.AddKeyframe(key2);
    cont.AddChannel(&opacityChannel);
    SumKeyframeFloat key1_Restored, key2_Restored;

    {
        // Create an output archive
        std::ofstream ofs(fileName);
        boost::archive::text_oarchive ar(ofs);
        ar & cont; // KeyframeFloat data is not written.

        // if i do ar & opacityChannel; than keyframeFloat data is written in archive

    }

    Container c_Restored ;
    SumOpacity_Channel opacityChannel_Restored;

    //load data
    {
        //create an input stream
        std::ifstream ifs(fileName);
        boost::archive::text_iarchive ar(ifs);
        ar & c_Restored ;
    }

    do
    {
        std::cout << '\n' << "Press a key to continue...";
    } while (std::cin.get() != '\n');
}

这是容器类
#pragma once

#include <boost/serialization/base_object.hpp>
#include <boost/serialization/split_member.hpp>
#include "SumChannel.h"
#include "SumOpacityChannel.h"
#include <boost/serialization/vector.hpp>
#include <memory>
#include <boost/serialization/export.hpp>



class Container
{
private:
    std::vector< SumOpacity_Channel* > Channels;

public:

    Container() {} ;

    ~Container()
    {
        if(Channels.size() > 0 )
            for (int i = 0; i < Channels.size(); i++)
            {
                delete Channels[i];
            }
    }




    Container(const Container& c)
    {
        if (Channels.size() > 0)
            Channels.clear(); // clear any previous channels

        for (int i = 0; i < c.Channels.size(); i++)
        {
            Channels.push_back(c.Channels[i]->Clone());
        }

    }

    Container& operator=(const Container& c)
    {
        if (Channels.size() > 0)
            Channels.clear(); // clear any previous channels

        for (int i = 0; i < c.Channels.size(); i++)
        {
            Channels.push_back(c.Channels[i]->Clone());
        }
        return *this;
    }


    void AddChannel(SumOpacity_Channel* channel)
    {
        Channels.push_back(channel->Clone());
    }


private:

    friend class boost::serialization::access;

    template <typename Archive>
    void save(Archive& ar, const unsigned version) const {
        ar  & Channels;
    }

    template <typename Archive>
    void load(Archive& ar, const unsigned version) {

        ar  & Channels;
    }

    BOOST_SERIALIZATION_SPLIT_MEMBER()

};

这是keyframeFloat类。
#pragma once
#include "KeyframeBase.h"
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/serialization.hpp>

class SumKeyframeFloat
{
private:
    float x;

public:

    SumKeyframeFloat() { this->x = 0.0; };
    SumKeyframeFloat(float xvalue) : x(xvalue) { };
    SumKeyframeFloat(const SumKeyframeFloat& key) : x ( key.x) { };
    ~SumKeyframeFloat() = default;

private:

    friend class boost::serialization::access;

    template<typename Archive>
    void save(Archive& ar, const unsigned int version) const {
        std::cout << "writing float keyframe to Archive" << std::endl;

        ar & x;
    }

    template<typename Archive>
    void load(Archive& ar, const unsigned int version) {


        ar & x;
    }

    BOOST_SERIALIZATION_SPLIT_MEMBER()

};

这是不透明度类标题和定义的总和
#include <string>
#include "SumChannel.h"
#include <iostream>
#include <vector>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>

class SumOpacity_Channel
{
private:
    std::vector<SumKeyframeFloat>  keyframes;

public:
    SumOpacity_Channel();
    SumOpacity_Channel(const SumOpacity_Channel &opacityChannel);
    ~SumOpacity_Channel() = default;
    SumOpacity_Channel& operator=(const SumOpacity_Channel &posChannel);
    void AddKeyframe(SumKeyframeFloat key);
    void DeleteKeyframe(SumKeyframeFloat key, int number);
    SumOpacity_Channel* Clone() const;


public:
    friend class boost::serialization::access;

    template<typename Archive>
    void save(Archive& ar, const unsigned int version) const {
        ar & keyframes;
    }

    template<typename Archive>
    void load(Archive& ar, const unsigned int version) {
        ar & keyframes;
    }


    BOOST_SERIALIZATION_SPLIT_MEMBER()

};

/////////////////////////////////////////////////////
#include "pch.h"
#include "SumOpacityChannel.h"


SumOpacity_Channel::SumOpacity_Channel()
{

}
/////////////////////////////////////////////////////////////////////////////////////////////

    SumOpacity_Channel::SumOpacity_Channel(const SumOpacity_Channel &opacityChannel)
    {
        this->keyframes.clear();
        for (auto KeyFrame : keyframes)
        {
            this->keyframes.push_back(KeyFrame);
        }
    }

    SumOpacity_Channel& SumOpacity_Channel::operator=(const SumOpacity_Channel &opacityChannel)
    {
        for (auto KeyFrame : keyframes)
        {
            this->keyframes.push_back(KeyFrame);
        }
        return *this;
    }

    void SumOpacity_Channel::AddKeyframe(SumKeyframeFloat key)
    {
        keyframes.push_back(key);
    }

    void SumOpacity_Channel::DeleteKeyframe(SumKeyframeFloat key, int number)
    {
        keyframes.erase(keyframes.begin() + number);
    }

    SumOpacity_Channel* SumOpacity_Channel::Clone() const
    {
        return  new SumOpacity_Channel(*this);
    }

最佳答案

您的频道为空。

此副本构造函数没有任何意义:

 SumOpacity_Channel::SumOpacity_Channel(const SumOpacity_Channel &opacityChannel)
    {
        this->keyframes.clear(); // [1]
        for (auto KeyFrame : keyframes) // [2]
        {
            this->keyframes.push_back(KeyFrame);
        }
    }

在[2]中,您正在遍历空 vector -清除了上面的行。
为什么还要调用clear?它是构造函数,因此此 vector 以前无法填充。在副本分配运算符中调用clear是有意义的,但此处不适用。

所以这
cont.AddChannel(&opacityChannel);

接着就,随即
void AddChannel(SumOpacity_Channel* channel)
{
    Channels.push_back(channel->Clone());
}

SumOpacity_Channel* SumOpacity_Channel::Clone() const
{
    return  new SumOpacity_Channel(*this);
}

给出空 vector 。
auto KeyFrame : keyframes

应该
auto KeyFrame : opacityChannel.keyframes

10-08 09:16