gpt4 book ai didi

c++ - Boost 在序列化过程中丢失了一些数据

转载 作者:行者123 更新时间:2023-11-30 04:43:49 25 4
gpt4 key购买 nike

在我的设置中,我将 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()

};

这是总和 Opacity 类头和定义

#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);
}

最佳答案

您的 channel 是空的。

这个复制构造函数没有任何意义:

 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

?

关于c++ - Boost 在序列化过程中丢失了一些数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58026818/

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