我试图序列化多态向量,但不同的尝试有不同的问题。整个事件顺序如下:
我有类派生(以及DerivedB、DerivedC等),它来自类库&一个包含虚拟库向量的类LotsOfBases。
虽然,我不认为这会导致这个问题-我相信我的问题是因为来自服务器的向量中的对象是以特定的顺序(AAABBCCCCD)出现的,当它们返回时,它们是以随机顺序出现的,并且可能有不同数量的派生类(ABABCDDDA)。
下面是我失败的尝试。使用下面的方法2,如果幸运的话,我可以来回发送信息(如果类顺序保持不变),但是当类类型更改顺序时,问题就开始发生了。
使用的代码&编译/运行时错误:
ar.template register_type<Derived>() ; - 注册类在“LotsOfBases.h”中的序列化函数,并在RunTime中得到以下调用:Error @ RunTime: what(): Input Stream Error --这是我最成功的地方,主要是上面提到的。ar.register_type<static...,但是我得到了编译错误,说明它是一个函数(在StackOverflow上看到了其他地方)BOOST_CLASS_EXPORT(Derived) ;位于".h“文件的末尾,它为Base的每个子类提供n个警告,但无法编译。错误:multiple definition of ``boost::archive::detail::extra_detail::init_guid<Derived>::g'BOOST_CLASS_EXPORT_IMPLEMENT(TextQuestion)来自导出类序列化 -与6 iirc相同的错误。以下是我的代码的缩短版本(没有从其他地方使用编辑):
类的代码
LotsOfBases:
#include "s11n.h" //Import All Serialization Headers In Correct Order
namespace boost { namespace serialization { class access ; } }
class LotsOfBases
{
public:
std::vector<Base *> getAllBases() ;
protected:
std::vector<Base *> allBases() ;
friend class boost::serialization::access ;
template <typename Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
ar & allBases ;
}
} ;基地:
#include "s11n.h" //Import All Serialization Headers In Correct Order
namespace boost { namespace serialization { class access ; } }
class Base
{
public:
Base() ;
~Base() ;
virtual std::string getBaseLocation() ;
protected:
std::string baseLocation ;
friend class boost::serialization::access ;
template <typename Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
ar & baseLocation ;
}
} ;导出
#include "s11n.h" //Import All Serialization Headers In Correct Order
namespace boost { namespace serialization { class access ; } }
class Derived
{
public:
Derived() ;
bool getIsAttackableBase() ;
private:
bool isAttackableBase ;
typedef Base _super;
friend class boost::serialization::access ;
template <typename Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
ar & boost::serialization::base_object<_super>(*this) ;
ar & isAttackableBase ;
}我肯定不会那么难的。我想我的问题是..。我做错了什么?我现在应该从哪里开始阅读/研究?
发布于 2015-11-29 05:06:43
我和您一样,已经搜索了相当长一段时间关于如何序列化多态数据,您的问题提供了足够的信息来帮助我理解我的错误,并纠正您的错误。由于您的类没有完全实现,所以我提供了一个代码示例,以实现您想要的结果。在我的例子中
#pragma once
#include <iostream>
#include <vector>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/stream.hpp>
using namespace std;
class Parent; // Forward declares
class Child; // so my classes are in your order是您的LotsOfBases
class Family {
friend class boost::serialization::access;
public:
Family() { ; }
~Family() { ; }
vector<Parent*> m_members;
//////////////////////////////////////////////////////////////////////
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar.template register_type<Child>();
ar & m_members;
}
//////////////////////////////////////////////////////////////////////
};父是您的Base
class Parent {
friend class boost::serialization::access;
public:
Parent() : m_name("") { ; }
Parent(string name) : m_name(name) { ; }
~Parent() { ; }
virtual string GetName() { return m_name; }
private:
string m_name;
//////////////////////////////////////////////////////////////////////
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & m_name;
}
//////////////////////////////////////////////////////////////////////
};Child是派生的
class Child : public Parent {
friend class boost::serialization::access;
public:
Child() : Parent(), m_age(0) { ; }
Child(string name, int id) : Parent(name), m_age(id) { ; }
~Child() { ; }
int m_age;
private:
//////////////////////////////////////////////////////////////////////
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<Parent>(*this);
ar & m_age;
}
//////////////////////////////////////////////////////////////////////
};主(为了完整性)
int main() {
Child *timmy = new Child("Timmy", 4);
Family JohnsonFamily;
JohnsonFamily.m_members.push_back(timmy);
// serialize object into a std::string
std::string serial_str;
boost::iostreams::back_insert_device<std::string> inserter(serial_str);
boost::iostreams::stream<boost::iostreams::back_insert_device<std::string>> s(inserter);
boost::archive::binary_oarchive oa(s);
oa & JohnsonFamily;
s.flush();
// read object backout of standard string into new Family object
boost::iostreams::basic_array_source<char> device(serial_str.data(), serial_str.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > t(device);
boost::archive::binary_iarchive ia(t);
Family FosterFamily;
ia & FosterFamily;
auto baseptr = FosterFamily.m_members[0];
auto child = dynamic_cast<Child*>(baseptr);
if (child != nullptr) {
cout << "Derived type infered from serialized base pointer." << endl;
cout << child->GetName() << " is " << child->m_age << endl;
}
cin.get();
return 0;
}我注意到您的派生程序实际上并没有继承,这肯定会导致问题。另外,关键是如果类中有一个多态容器,那么必须在该类中(而不是在基类中)注册每个派生类型。请看我以上的家庭课。
希望这能帮到你。
https://stackoverflow.com/questions/17698267
复制相似问题