我想以此为榜样http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/serialization.html#constructors但我不断收到错误.在示例之后,我在尝试访问私有变量时遇到错误(足够公平):
bs.cpp:10: error: ‘const int my_class::m_attribute’ is private
Run Code Online (Sandbox Code Playgroud)
但是,如果我将save_construct_data添加为朋友,则会出现歧义错误:
/usr/include/boost/serialization/serialization.hpp:148: error: call of overloaded ‘save_construct_data(boost::archive::text_oarchive&, const my_class*&, const boost::serialization::version_type&)’ is ambiguous
/usr/include/boost/serialization/serialization.hpp:83: note: candidates are: void boost::serialization::save_construct_data(Archive&, const T*, unsigned int) [with Archive = boost::archive::text_oarchive, T = my_class]
bs.cpp:10: note: void boost::serialization::save_construct_data(Archive&, const my_class*, unsigned int) [with Archive = boost::archive::text_oarchive]
bs.cpp:29: note: void boost::serialization::save_construct_data(Archive&, const my_class*, unsigned int) [with Archive = boost::archive::text_oarchive]
Run Code Online (Sandbox Code Playgroud)
我可以将函数定义移动到friend声明中,但这只是丑陋的.
我接下来应该尝试什么?
谢谢,杰恩
我想使用 BOOST 序列化/反序列化以下向量中对象的值(而不是指针):
std :: vector <A*> m_vector;
Run Code Online (Sandbox Code Playgroud)
要序列化,我使用以下代码:
int nItems = m_vector.size();
ar & nItems;
std::for_each(m_vector.begin(), m_vector.end(), [&ar](A* pItem) {
ar & *pItem;
});
Run Code Online (Sandbox Code Playgroud)
并反序列化:
int nItems;
ar & nItems;
for (int i = 0; i < nItems; ++i) {
A* pItem;
ar & *pItem; ///////////// Run-Time Check Failure #3
m_vector.push_back(pItem);
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行程序时,出现以下错误:
Run-Time Check Failure # 3 - The variable 'pItem' is Being Used without Being initialized.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
谢谢你。
通过在类的命令中调用保存函数(ala boost-serialize)可能会产生什么负面/未定义的行为?
我在某处读到了必须编译boost的序列化库(我忘了在哪里阅读它,否则我会发布一个链接).
所以我从source forge下载了最新版本并将其解压缩到我项目中的路径.现在?
我调查了这个文件夹,但我找不到了makefile.
那么我该怎么做才能编译boost:serialization lib?
编辑:然而我尝试使用它,而不编译它,但我得到这个错误:
boost/archive/basic_xml_oarchive.hpp:92:9: error:
no matching function for call to 'assertion_failed'
BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
所以我认为原因是它没有编译.是对的吗?
以下代码编译得很好:
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <memory>
struct A {
int i;
A(): i(0) {}
A(int i): i(i) {}
template <typename Archive>
void serialize(Archive& ar, const unsigned int) {
ar & i;
}
};
int main() {
auto a = std::make_shared<A>(465);
std::stringstream stream;
boost::archive::text_oarchive out{stream};
out << a;
}
Run Code Online (Sandbox Code Playgroud)
现在我希望如果我替换A它int那么它也应该工作.
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <memory>
int main() {
auto a = std::make_shared<int>(465);
std::stringstream stream;
boost::archive::text_oarchive out{stream};
out …Run Code Online (Sandbox Code Playgroud) 我想使用boost :: archive :: iterators :: base64_from_binary.但我似乎无法弄清楚为什么它存在于"存档"之下.这意味着什么?我是否应该因为任何原因而使用此代码?
谢谢 -
当我尝试使用受保护的成员序列化类时,我收到以下错误:"无法访问类NetElement中声明的受保护成员".我的想法是,我希望在类定义之外有一个序列化函数.我究竟做错了什么?
最好的问候,mayydodol
这是代码......
// class definition
class NetElement
{
friend class boost::serialization::access;
protected:
int nelements;
int ids;
public:
static NetElement* New(){return new NetElement;}
virtual void Delete(){delete this;}
protected:
NetElement(){};
~NetElement(){};
};
// nonintrusive serialize
template<class Archive>
void serialize(Archive & ar, NetElement& element, const unsigned int version=1)
{
ar & element.nelements & element.ids;
}
int main(void)
{...
std::ofstream os("Pipe1.txt");
boost::archive::text_oarchive oa(os);
serialize(oa,el/*ref to NetElementObj*/);
...
}
Run Code Online (Sandbox Code Playgroud) 我有三个班:
class A
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & this->id & this->somefield;
}
protected:
A() {} // just for boost, normally I create this class with non-default constructor
int id;
Sometype somefield;
public:
A(unsigned int id);
// blah blah, also some virtual methods
};
// _____________________ CLASS B
class B : public A
{
private:
friend class boost::serialization::access;
template<class Archive> inline friend void load_construct_data(Archive &ar, B *t, const …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用 boost::serialization 来记录对象。例如,我使用以下代码来序列化一个对象:
struct Abc
{
int a;
float b;
double c;
};
namespace boost
{
namespace serialization
{
template <typename Archive>
void serialize(Archive& ar, Abc &obj,const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(obj.a);
ar & BOOST_SERIALIZATION_NVP(obj.b);
ar & BOOST_SERIALIZATION_NVP(obj.c);
};
}
};
Abc kkk;
std::ofstream ofs(tmpFile);
boost::archive::xml_oarchive ar(ofs);
ar & kkk;
ofs.close();
Run Code Online (Sandbox Code Playgroud)
但是,当我编译代码时,出现以下编译错误:
Error 1 error C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type' \boost_1_50_0\boost\archive\basic_xml_oarchive.hpp 92
Run Code Online (Sandbox Code Playgroud)
错误信息引出boost的源码:
template<class T>
void save_override(T & t, BOOST_PFTO …Run Code Online (Sandbox Code Playgroud) 以下是我设计的结构和类,最终目标是使用 boost 序列化将值写入 xml 当我编译下面的代码时,我收到一个编译时错误(错误很大,我只是粘贴了它的最后一行这可能讨论了这个问题)
/boost_1_49_0/include/boost/serialization/access.hpp:118: 错误: 'class std::map, std::allocator >, std::map, std::allocator >, std::map, std::allocator >、IdInfo、std::less、std::allocator > >、std::allocator、std::allocator >、IdInfo> > >、std::less、std::allocator > >、std::allocator、std ::allocator >, std::map, std::allocator >, IdInfo, std::less, std::allocator > >, std::allocator, std::allocator >, IdInfo> > > > > >, std ::less、std::allocator > >、std::allocator、std::allocator >、std::map、std::allocator>、std::map、std::allocator>、IdInfo、std::less , std::allocator > >, std::allocator, std::allocator >, IdInfo> > >, std::less, std::allocator > >, std::allocator, std::allocator >, std::映射、std::allocator >、IdInfo、std::less、std::allocator > >、std::allocator、std::allocator >、IdInfo> > > > > > > >' 没有名为 'serialize' 的成员 …