提升多态类的序列化似乎不起作用(1.40+ boost),例如使用以下代码,我相信我遵循以下规则:导出类并尝试使用gcc4.4(ubuntu)和windows VS2010(使用boost 1.48) :在下面的程序中,我希望打印10和100,但它只打印10,这意味着它只序列化了基类;
我大多是从boost的文档中复制了这个例子,但它仍然不起作用; 有谁有任何想法?非常感谢LS
#include <iostream>
#include <sstream>
#include <boost/serialization/base_object.hpp>
#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);
}
};
class derived : public base {
public:
friend class boost::serialization::access;
int v2 ;
derived() { v2 = 100;}
template<class Archive>
void serialize(Archive & ar, const unsigned …Run Code Online (Sandbox Code Playgroud) 仅当选择了文本文件存档时,以下示例才会编译.随着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>::************)'
Run Code Online (Sandbox Code Playgroud)
任何提示?
#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 …Run Code Online (Sandbox Code Playgroud) 在我们的主项目中,构建管理推迟到CMake.对于我们不同的依赖项,一切都很顺利,包括Boost :: System,但是我们无法为Boost :: Serialization编译这个最小的例子.
# Untested with previous versions, yet should work
cmake_minimum_required(VERSION 2.8.11)
project(SerialCmake)
# Boost dependency
set(BOOST_ROOT CACHE PATH "Path to Boost library")
find_package(Boost 1.55 COMPONENTS serialization system)
# Create the target
add_executable(${PROJECT_NAME} main.cpp)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
class Serializable
{
friend class boost::serialization::access;
public:
Serializable(int a): a(a)
{}
private:
template <class Archive>
inline void serialize (Archive &ar, const unsigned int version)
{
ar & a;
}
int a;
};
int main(int …Run Code Online (Sandbox Code Playgroud) 这是我尝试过的,但我被Error: Unknown exception抛出了
try{//load
std::ifstream stream(arch_name.c_str());
std::cout << ">> " << "Started deserializing " << arch_name << std::endl;
boost::archive::binary_iarchive arc(stream);
arc & c & matrix;
stream.close();
std::cout << ">> " << "Finished deserializing" << std::endl;
}catch(std::exception e){
std::cout << "Error: " << e.what() << std::endl;
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这在使用gcc的Linux中运行良好.我在Windows中使用Visual Studio.
backtrace显示basic_binary_iprimitive.hpp正在抛出异常
template<class Archive, class Elem, class Tr>
inline void basic_binary_iprimitive<Archive, Elem, Tr>::load_binary( void *address, std::size_t count){
//.....
if(scount != s)
boost::serialization::throw_exception(
archive_exception(archive_exception::input_stream_error)
);
Run Code Online (Sandbox Code Playgroud)
我改变了 …
我正在尝试使用 boost 的功能来序列化指向原语的指针(这样我就不必自己取消引用并进行深度存储)。然而,当我尝试这样做时,我遇到了一堆错误。这是一个简单的类示例,该类应该包含save从load文件写入和读取类内容的方法。该程序无法编译:
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <fstream>
class A
{
public:
boost::shared_ptr<int> sp;
int const * p;
int const& get() {return *p;}
void A::Save(char * const filename);
static A * const Load(char * const filename);
//////////////////////////////////
// Boost Serialization:
//
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar,const unsigned int file_version)
{
ar & p & v;
}
};
// save the world to a file:
void A::Save(char …Run Code Online (Sandbox Code Playgroud) 传递this分配给另一个指针的指针可以很好地工作,但是直接传递它本身本身却不行,如下所示:
table_row* table_row::deserialize_row(std::string src_serialized_row) {
std::stringstream ss(src_serialized_row);
boost::archive::text_iarchive ia(ss);
table_row * dest = this;
ia >> dest; // this is fine, compiles.
return dest;
}
Run Code Online (Sandbox Code Playgroud)
table_row* table_row::deserialize_row(std::string src_serialized_row) {
std::stringstream ss(src_serialized_row);
boost::archive::text_iarchive ia(ss);
ia >> this; //error, >> operator does not match [error]
return this;
}
Run Code Online (Sandbox Code Playgroud)
[错误] 我不太了解。我在两个代码示例中都传递了相同的指针,对吗?为什么会出错?