以下示例仅在选择了文本文件存档时进行编译。随着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();
}
}
答案 0 :(得分:6)
静态断言失败是由这些行触发的:
- ar & boost::serialization::base_object<Base>(*this);
他们应该是:
+ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
exands to:
ar & boost::serialization::make_nvp( "Base",
boost::serialization::base_object<Base>(*this)
);