gpt4 book ai didi

boost-serialization - boost序列化使用xml存档编译问题

转载 作者:行者123 更新时间:2023-12-01 22:42:31 29 4
gpt4 key购买 nike

以下示例仅在选择文本文件存档时编译。选择 xml 文件会出现大量错误消息行。

我用 g++ 4.7.2 编译。

该示例包含两次以下行:boost::archive::xml_oarchive ar(ofs);boost::archive::text_oarchive ar(ofs);

xml 无效文字作品

( 我在这里只写了很多次相同的描述,因为我不能在这里发布少于 100 行的描述...:-( )

错误行之一是

    /usr/include/boost/archive/basic_xml_iarchive.hpp:70:9: error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<Base>::************)'

有什么提示吗?

    #include <iostream>

#include <assert.h>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>


#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_woarchive.hpp>
#include <boost/archive/xml_wiarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

using namespace std;

class Base
{
public:
int genericData;

Base(int _genericData) : genericData(_genericData) {}
Base() {}

friend class boost::serialization::access;

template<typename Archive>
void serialize(Archive& ar, const unsigned version)
{
ar & BOOST_SERIALIZATION_NVP( genericData );
}

virtual void Print() { cout << "Base only " << genericData << endl; }
};

BOOST_SERIALIZATION_ASSUME_ABSTRACT(Base)

class A: public Base
{
public:
int a;

A(int _generic, int _a):Base(_generic),a(_a){}
A(){};

template<typename Archive>
void serialize(Archive& ar, const unsigned version)
{
ar & boost::serialization::base_object<Base>(*this);
ar & BOOST_SERIALIZATION_NVP( a );
}
virtual void Print() { cout << "A " << genericData << " " << a << endl; }
};

class B: public Base
{
public:
int b1;
int b2;

B( int _generic, int _b1, int _b2): Base( _generic), b1(_b1), b2(_b2) {}
B() {}

template<typename Archive>
void serialize(Archive& ar, const unsigned version)
{
ar & boost::serialization::base_object<Base>(*this);
ar & BOOST_SERIALIZATION_NVP( b1 );
ar & BOOST_SERIALIZATION_NVP( b2 );
}
virtual void Print() { cout << "B " << genericData << " " << b1 << " " << b2 << endl; }
};

class C: public Base
{
public:
int c1;
int c2;
int c3;

C( int _generic, int _c1, int _c2, int _c3): Base( _generic), c1(_c1), c2(_c2), c3(_c3) {}
C() {}

template<typename Archive>
void serialize(Archive& ar, const unsigned version)
{
ar & boost::serialization::base_object<Base>(*this);
ar & BOOST_SERIALIZATION_NVP( c1 );
ar & BOOST_SERIALIZATION_NVP( c2 );
ar & BOOST_SERIALIZATION_NVP( c3 );
}
virtual void Print() { cout << "C " << genericData << " " << c1 << " " << c2 << " " << c3 << endl; }
};

class Ptr
{
public:
Ptr( Base* _dptr):dptr(_dptr) {}
Ptr(){}
Base *dptr;

template<typename Archive>
void serialize(Archive& ar, const unsigned version)
{
ar & BOOST_SERIALIZATION_NVP(dptr); // Simply serialize the data members of Obj
}

void Print()
{
cout << "Object Ptr Points to addr: " << dptr << " und enthaelt:" << endl;
dptr->Print();
}
};
#if 0
#include <boost/serialization/export.hpp>
BOOST_CLASS_EXPORT_GUID( A, "A");
BOOST_CLASS_EXPORT_GUID( B, "B");
BOOST_CLASS_EXPORT_GUID( C, "C");
#endif

int main()
{

{
ofstream ofs("test.data");
boost::archive::xml_oarchive ar(ofs);
//boost::archive::text_oarchive ar(ofs);

A a( 111, 1);
B b( 222, 2,20);
C c( 333, 3,30,300);

// Base a(1);
// Base b(2);
// Base c(3);

//Ptr p1(&a);
//Ptr p2(&b);
//Ptr p3(&c);

Base *p1( &a);
Base *p2( &b);
Base *p3( &c);

ar.register_type(static_cast<A *>(NULL));
ar.register_type(static_cast<B *>(NULL));
ar.register_type(static_cast<C *>(NULL));


ar & boost::serialization::make_nvp("dptr",p1);
ar & boost::serialization::make_nvp("dptr",p2);
ar & boost::serialization::make_nvp("dptr",p3);

p1->Print();
p2->Print();
p3->Print();

}

cout << "+++" << endl;


// Read back
{
ifstream ifs("test.data");
boost::archive::xml_iarchive ar(ifs);
//boost::archive::text_iarchive ar(ifs);

Base* p1;
Base* p2;
Base* p3;

ar.register_type(static_cast<A *>(NULL));
ar.register_type(static_cast<B *>(NULL));
ar.register_type(static_cast<C *>(NULL));

ar & boost::serialization::make_nvp("dptr",p1);
ar & boost::serialization::make_nvp("dptr",p2);
ar & boost::serialization::make_nvp("dptr",p3);


p1->Print();
p2->Print();
p3->Print();

}
}

最佳答案

静态断言失败是由这些行触发的:

- ar & boost::serialization::base_object<Base>(*this);

它们应该是:

+ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);

扩展为:

ar & boost::serialization::make_nvp( "Base",
boost::serialization::base_object<Base>(*this)
);

关于boost-serialization - boost序列化使用xml存档编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13274097/

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