gpt4 book ai didi

c++ - boost 序列化输入流错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:45 24 4
gpt4 key购买 nike

我正在研究一个简单的序列化类。我一直在输入流上抛出异常。我将以下示例放在一起,以简单的方式说明我试图完成的任务。

我有一个 boost 序列化的简单示例,但我遇到了异常:

#include <boost/serialization/serialization.hpp> 
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#define NVP(X) X

class base {
public:
friend class boost::serialization::access;
base (){ v1 = 10;}
int v1;
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{

ar & NVP(v1);
}
virtual void bla()=0;
};


class derived : public base {
public:
friend class boost::serialization::access;
int v2 ;
derived() { v2 = 100;}
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version){
boost::serialization::base_object<base>(* this);
ar & NVP(v2);
}

virtual void bla(){};
};
BOOST_CLASS_EXPORT(base);
BOOST_CLASS_EXPORT_GUID(derived, "derived");

int main ( )
{
std::stringstream ss;
boost::archive::text_oarchive ar(ss);
base *b = new derived();
ar << NVP(b);
std::cout << ss.str()<<std::endl;



std::istringstream ssi;
base *b1 = new derived();
{
boost::archive::text_iarchive ar1(ssi);
ar1 >> b1;
}
//std::cout << ssi.str();
std::cout << "v1: " << b1->v1 << std::endl;

}

我得到的异常是:

terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error

如有任何帮助,我们将不胜感激。

最佳答案

您正在从空流中读取:

std::istringstream ssi;
// ...
boost::archive::text_iarchive ar1(ssi);

另外,你泄露了这个对象:

base *b1 = new derived();

这是一个固定的例子,注释:

  • 在使用流数据之前关闭存档是非常好的做法/很重要
  • BOOST_CLASS_EXPORT_GUID(derived, "derived")不添加任何超出 BOOST_CLASS_EXPORT(derived) 的内容
  • 您可以有条件地打印 v2:

    if (auto* d = dynamic_cast<derived*>(b1))
    std::cout << "v2: " << d->v2 << std::endl;
  • 我用过 bla()作为打印值的示例

  • NVP()那里有点不确定。为什么不把它留给非标记文件(即 XML 除外)?如果您打算支持 XML,只需使用 BOOST_SERIALIZATION_NVP , boost::serialization::make_nvp等等

  • std::cout << "v2: " << b1->v2 << std::endl;完全不合适

  • 只需初始化b1为空,这样你就不会泄露它;记得释放所有指针(使用智能指针!)

  • public: 的组合和 friend在你的类型中并没有多大意义

Live On Coliru

#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <sstream>

class base {
public:
base(int v1) : v1(v1) {}
virtual void bla() const = 0;

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

template <class Archive> void serialize(Archive &ar, unsigned /*int const file_version*/) {
ar & BOOST_SERIALIZATION_NVP(v1);
}
protected:
int v1;
};

class derived : public base {
public:
derived(int v1 = 10, int v2 = 100) : base(v1), v2(v2) {}
virtual void bla() const {
std::cout << "v1: " << v1 << ", v2: " << v2 << "\n";
}

private:
friend class boost::serialization::access;
int v2;
template <class Archive> void serialize(Archive &ar, unsigned /*int const file_version*/) {
boost::serialization::base_object<base>(*this);
ar & BOOST_SERIALIZATION_NVP(v2);
}
};

BOOST_CLASS_EXPORT(base)
BOOST_CLASS_EXPORT(derived)

int main() {
std::stringstream ss;
{
boost::archive::text_oarchive ar(ss);
base *b = new derived();
ar << boost::serialization::make_nvp("base", b);

delete b; // TODO use RAII instead
}

std::cout << ss.str() << std::endl;
base *deserialized = nullptr;
{
boost::archive::text_iarchive ar1(ss);
ar1 >> boost::serialization::make_nvp("base", deserialized);
}

deserialized->bla();
delete deserialized;
}

打印

22 serialization::archive 12 0 7 derived 1 0
0 100

v1: 10, v2: 100

关于c++ - boost 序列化输入流错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30181004/

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