这是我类(class)的样子:
#include <iostream>
#include <boost/dynamic_bitset/dynamic_bitset.hpp>
#include <vector>
#include <fstream>
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;
using namespace boost;
class outer{
friend class boost::serialization::access;
public:
int a;
class inner{
friend class boost::serialization::access;
public:
int a;
inner(int a){
this->a = a;
}
template<class Archive>
void serialize(Archive & ar,const unsigned int version){
ar & a;
}
inner(){
}
};
vector<inner> inners;
outer(int a){
this->a = a;
inners.push_back(inner(this->a+1));
}
outer(){
}
template<class Archive>
void serialize(Archive & ar,const unsigned int version){
ar & a;
ar & inners;
}
};
vector<outer> outers;
vector<outer> _outers;
BOOST_CLASS_EXPORT(outer);
int main(int, char*[]) {
int i;
std::ofstream ofs("filename.dat");
for(i=0;i<5;i++)
outers.push_back(outer(i));
boost::archive::text_oarchive oa(ofs);
oa << outers;
std::ifstream ifs("filename.dat");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> _outers;
return EXIT_SUCCESS;
}
我创建了一个 outer 类实例的 vector ,并将其写出到一个文本流中(这似乎工作正常)。但是当我读回它时,我得到了错误:
在抛出“boost::archive::archive_exception”实例后调用终止
what(): 输入流错误
以上是完整的自包含代码,编译时使用:
g++ -I /path/to/boost test.cpp -lboost_serialization
我该如何解决这个问题,有什么想法吗?
最佳答案
缺少:
#include <boost/serialization/export.hpp>
在回读文件之前需要关闭输出存档/流:
int main() {
{
std::ofstream ofs("filename.dat");
std::vector<outer> outers(5);
std::iota(outers.begin(), outers.end(), 0u);
{
boost::archive::text_oarchive oa(ofs);
oa << outers;
}
}
{
// read class state from archive
std::vector<outer> _outers;
std::ifstream ifs("filename.dat");
boost::archive::text_iarchive ia(ifs);
ia >> _outers;
for(auto& outer : _outers) {
std::cout << "outer " << outer.a << "\n";
for (auto& inner: outer.inners)
std::cout << " inner " << inner.a << "\n";
}
}
}
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <fstream>
class outer {
friend class boost::serialization::access;
public:
int a;
class inner {
friend class boost::serialization::access;
public:
int a;
inner(int a) { this->a = a; }
template <class Archive> void serialize(Archive &ar, unsigned) { ar &a; }
inner() {}
};
std::vector<inner> inners;
outer(int a = 0) : a(a), inners {a+1} { }
template <class Archive> void serialize(Archive &ar, unsigned) {
ar &a;
ar &inners;
}
};
BOOST_CLASS_EXPORT(outer)
int main(int, char *[]) {
{
std::ofstream ofs("filename.dat");
std::vector<outer> outers(5);
std::iota(outers.begin(), outers.end(), 0u);
{
boost::archive::text_oarchive oa(ofs);
oa << outers;
}
}
{
// read class state from archive
std::vector<outer> _outers;
std::ifstream ifs("filename.dat");
boost::archive::text_iarchive ia(ifs);
ia >> _outers;
for(auto& outer : _outers) {
std::cout << "outer " << outer.a << "\n";
for (auto& inner: outer.inners)
std::cout << " inner " << inner.a << "\n";
}
}
}
打印
outer 0
inner 1
outer 1
inner 2
outer 2
inner 3
outer 3
inner 4
outer 4
inner 5
关于c++ - Boost序列化无法恢复保存的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30222898/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳