Boost Serialization throws exception while reading from binary - c++

I have found this code and trying to modify it for binary object serialization on visual studios. But on doing so it throws exception
"Unhandled exception at 0x00007FFF269954D8 in Boost_Serialization.exe: Microsoft C++ exception: std::length_error at memory location 0x000000829D94F7B0."
Not sure what's wrong.
#include <iostream>
#include <vector>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include<boost/archive/binary_oarchive.hpp>
#include<boost/archive/binary_iarchive.hpp>
#include<boost/serialization/binary_object.hpp>
class Info
{
private:
// Allow serialization to access non-public data members.
friend class boost::serialization::access;
//template<class Archive>
//void save(Archive & ar, const unsigned int version) const { ar & filenames;}
template<class Archive>
void load(Archive & ar, const unsigned int version) { ar & filenames; }
BOOST_SERIALIZATION_SPLIT_MEMBER()
std::vector<std::string> filenames;
public:
void AddFilename(const std::string& filename);
void Print() const;
};
void Info::Print() const { std::copy(filenames.begin(),filenames.end(),std::ostream_iterator<std::string>(std::cout, "\n")); }
void Info::AddFilename(const std::string& filename) { filenames.push_back(filename); }
int main(int argc, char** argv)
{
std::vector<Info> infs;
Info info1, info2;
info1.AddFilename("ThisFile.txt");
info2.AddFilename("ThatFile.txt");
info2.AddFilename("OtherFile.txt");
info2.AddFilename("ABC");
info2.AddFilename("123");
info2.AddFilename("XYZ");
infs.push_back(info1);
infs.push_back(info2);
// Save filename data contained in Info object
{
std::ofstream Obj_ofstream("data.dat", std::ios::binary);
boost::archive::binary_oarchive op_archive(Obj_ofstream);
op_archive << boost::serialization::make_binary_object(&infs, sizeof(infs));
//Obj_ofstream.close();
}
// Restore from saved data and print to verify contents
std::vector<Info> restored_info;
{
std::ifstream Obj_ifstream("data.dat", std::ios::binary);
boost::archive::binary_iarchive ip_archive(Obj_ifstream);
ip_archive >> restored_info;
//Obj_ifstream.close();
}
//std::vector<Info>::const_iterator it = restored_info.begin();
//for (; it != restored_info.end(); ++it)
//{
// Info info = *it;
// info.Print();
//}
std::cout << "Testing : " << std::endl;
return 0;

You use BOOST_SERIALIZATION_SPLIT_MEMBER() and do not implement save method. However, you do not need to do that. Remove splitting and implement general void serialize(Archive & ar, const unsigned int version) method (note to include new header for vector serialization). It will look like this and run like a charm:
#include <iostream>
#include <vector>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include<boost/archive/binary_oarchive.hpp>
#include<boost/archive/binary_iarchive.hpp>
#include<boost/serialization/binary_object.hpp>
#include<boost/serialization/vector.hpp>
class Info
{
private:
// Allow serialization to access non-public data members.
friend class boost::serialization::access;
//template<class Archive>
//void save(Archive & ar, const unsigned int version) const { ar & filenames;}
template<class Archive>
void serialize(Archive & ar, const unsigned int version) { ar & filenames; }
std::vector<std::string> filenames;
public:
void AddFilename(const std::string& filename);
void Print() const;
};
void Info::Print() const { std::copy(filenames.begin(), filenames.end(), std::ostream_iterator<std::string>(std::cout, "\n")); }
void Info::AddFilename(const std::string& filename) { filenames.push_back(filename); }
int main(int argc, char** argv)
{
std::vector<Info> infs;
Info info1, info2;
info1.AddFilename("ThisFile.txt");
info2.AddFilename("ThatFile.txt");
info2.AddFilename("OtherFile.txt");
info2.AddFilename("ABC");
info2.AddFilename("123");
info2.AddFilename("XYZ");
infs.push_back(info1);
infs.push_back(info2);
// Save filename data contained in Info object
{
std::ofstream Obj_ofstream("data.dat", std::ios::binary);
boost::archive::binary_oarchive op_archive(Obj_ofstream);
op_archive << infs;
//Obj_ofstream.close();
}
// Restore from saved data and print to verify contents
std::vector<Info> restored_info;
{
std::ifstream Obj_ifstream("data.dat", std::ios::binary);
boost::archive::binary_iarchive ip_archive(Obj_ifstream);
ip_archive >> restored_info;
//Obj_ifstream.close();
}
std::vector<Info>::const_iterator it = restored_info.begin();
for (; it != restored_info.end(); ++it)
{
Info info = *it;
info.Print();
}
std::cout << "Testing : " << std::endl;
system("PAUSE");
return 0;
}

Related

boost::serialization of static array / archive exception on restore

The following code compiles and seems to serialize properly (that is, the static is saved only once apparently). However, it has an 'input stream error' exception on restore:
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/level.hpp>
#include <boost/serialization/array.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <iostream>
#include <fstream>
#include <array>
class SA {
std::array<char, 1024*1024> sbuf;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int ver) {
ar & boost::serialization::make_array(sbuf.data(), sbuf.size());
};
};
BOOST_CLASS_IMPLEMENTATION(SA, boost::serialization::object_serializable); // serialization_level
BOOST_CLASS_TRACKING(SA, boost::serialization::track_always); // tracking_level
class Foo {
char buf[1024];
inline static SA sxbuf;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int ver) {
ar & boost::serialization::make_array(buf, sizeof(buf));
ar & sxbuf;
};
};
class FooList {
std::array<Foo, 100> fool;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int ver) {
ar & boost::serialization::make_array(fool.data(), fool.size());
};
};
int
main()
{
const std::string filename{"foo.sav"};
FooList x;
std::ofstream out{filename, std::ios::binary};
boost::archive::binary_oarchive oa(out);
oa << x;
std::cout << "Saved\n";
std::ifstream ifs{filename};
boost::archive::binary_iarchive ia(ifs);
if (ifs.fail()) {
std::cerr << "couldn't open input file " << filename << "\n";
return 1;
}
ia >> x; // gives exception
std::cout << "Restored\n";
return 0;
}
The output is as follows:
Saved
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
(I tried this on godbolt too, but -lboost_serialization didn't seem to work, so it wouldn't link properly... possibly my error there).
Any insight much appreciated...
This opens an archive before the writing end is finalized:
std::ofstream out{filename, std::ios::binary};
boost::archive::text_oarchive oa(out);
oa << x;
std::cout << "Saved\n";
std::ifstream ifs{filename};
Also ifs is missing the ios::binary flag.
Other than that, you can
drop make_array,
prefer std::array over T[] and
default the serialization level.
Live On Coliru
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/array.hpp>
#include <array>
#include <fstream>
#include <iostream>
class SA {
std::array<char, 1024 * 1024> sbuf;
friend class boost::serialization::access;
template <class Ar> void serialize(Ar &ar, unsigned) { ar &sbuf; }
};
BOOST_CLASS_TRACKING(SA, boost::serialization::track_always) // tracking_level
class Foo {
std::array<char, 1024> buf;
inline static SA sxbuf;
friend class boost::serialization::access;
template <class Ar> void serialize(Ar &ar, unsigned) { ar &buf &sxbuf; }
};
class FooList {
std::array<Foo, 100> fool;
friend class boost::serialization::access;
template <class Ar> void serialize(Ar &ar, unsigned) { ar &fool; }
};
int main() {
const std::string filename{"foo.sav"};
{
std::ofstream out(filename, std::ios::binary);
boost::archive::binary_oarchive oa(out);
FooList x{};
oa << x;
std::cout << "Saved\n";
}
{
std::ifstream ifs(filename, std::ios::binary);
boost::archive::binary_iarchive ia(ifs);
FooList x{};
ia >> x; // gives exception
std::cout << "Restored\n";
}
}
Prints
Saved
Restored

Boost missing some data during serialization process

In my setup i have vector of OpacityChannel pointer as a member of Container class.
if i create a varible of OpacityChannel and write it to the archive than everything is written as expected.
when i write Container class object and write it to archive than it misses the data for KeyframeFloat class which is a member of OpacityChannel class.
This is my main file if i change the line from ar & cont; to ar & opacityChannel than it writes the data as required.
I am not able to understand why it is missing data when i write Container class.
#include "pch.h"
#include <iostream>
#include<fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "Container.h"
#include "KeyframeFloat.h"
#include <boost/serialization/export.hpp>
BOOST_CLASS_EXPORT_GUID(SumOpacity_Channel, "SumOpacity_Channel")
BOOST_CLASS_EXPORT_GUID(SumKeyframeFloat, "SumKeyframeFloat")
int main()
{
const char* fileName = "saved.txt";
std::vector<int> vec;
Container cont;
SumOpacity_Channel opacityChannel;
SumKeyframeFloat key1, key2;
opacityChannel.AddKeyframe(key1);
opacityChannel.AddKeyframe(key2);
cont.AddChannel(&opacityChannel);
SumKeyframeFloat key1_Restored, key2_Restored;
{
// Create an output archive
std::ofstream ofs(fileName);
boost::archive::text_oarchive ar(ofs);
ar & cont; // KeyframeFloat data is not written.
// if i do ar & opacityChannel; than keyframeFloat data is written in archive
}
Container c_Restored ;
SumOpacity_Channel opacityChannel_Restored;
//load data
{
//create an input stream
std::ifstream ifs(fileName);
boost::archive::text_iarchive ar(ifs);
ar & c_Restored ;
}
do
{
std::cout << '\n' << "Press a key to continue...";
} while (std::cin.get() != '\n');
}
This is the container Class
#pragma once
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/split_member.hpp>
#include "SumChannel.h"
#include "SumOpacityChannel.h"
#include <boost/serialization/vector.hpp>
#include <memory>
#include <boost/serialization/export.hpp>
class Container
{
private:
std::vector< SumOpacity_Channel* > Channels;
public:
Container() {} ;
~Container()
{
if(Channels.size() > 0 )
for (int i = 0; i < Channels.size(); i++)
{
delete Channels[i];
}
}
Container(const Container& c)
{
if (Channels.size() > 0)
Channels.clear(); // clear any previous channels
for (int i = 0; i < c.Channels.size(); i++)
{
Channels.push_back(c.Channels[i]->Clone());
}
}
Container& operator=(const Container& c)
{
if (Channels.size() > 0)
Channels.clear(); // clear any previous channels
for (int i = 0; i < c.Channels.size(); i++)
{
Channels.push_back(c.Channels[i]->Clone());
}
return *this;
}
void AddChannel(SumOpacity_Channel* channel)
{
Channels.push_back(channel->Clone());
}
private:
friend class boost::serialization::access;
template <typename Archive>
void save(Archive& ar, const unsigned version) const {
ar & Channels;
}
template <typename Archive>
void load(Archive& ar, const unsigned version) {
ar & Channels;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
This is the keyframeFloat class.
#pragma once
#include "KeyframeBase.h"
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/serialization.hpp>
class SumKeyframeFloat
{
private:
float x;
public:
SumKeyframeFloat() { this->x = 0.0; };
SumKeyframeFloat(float xvalue) : x(xvalue) { };
SumKeyframeFloat(const SumKeyframeFloat& key) : x ( key.x) { };
~SumKeyframeFloat() = default;
private:
friend class boost::serialization::access;
template<typename Archive>
void save(Archive& ar, const unsigned int version) const {
std::cout << "writing float keyframe to Archive" << std::endl;
ar & x;
}
template<typename Archive>
void load(Archive& ar, const unsigned int version) {
ar & x;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
This is sum Opacity class header and defination
#include <string>
#include "SumChannel.h"
#include <iostream>
#include <vector>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
class SumOpacity_Channel
{
private:
std::vector<SumKeyframeFloat> keyframes;
public:
SumOpacity_Channel();
SumOpacity_Channel(const SumOpacity_Channel &opacityChannel);
~SumOpacity_Channel() = default;
SumOpacity_Channel& operator=(const SumOpacity_Channel &posChannel);
void AddKeyframe(SumKeyframeFloat key);
void DeleteKeyframe(SumKeyframeFloat key, int number);
SumOpacity_Channel* Clone() const;
public:
friend class boost::serialization::access;
template<typename Archive>
void save(Archive& ar, const unsigned int version) const {
ar & keyframes;
}
template<typename Archive>
void load(Archive& ar, const unsigned int version) {
ar & keyframes;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
////////////////////////////////////////////////
#include "pch.h"
#include "SumOpacityChannel.h"
SumOpacity_Channel::SumOpacity_Channel()
{
}
/////////////////////////////////////////////////////////////////////////////////////////////
SumOpacity_Channel::SumOpacity_Channel(const SumOpacity_Channel &opacityChannel)
{
this->keyframes.clear();
for (auto KeyFrame : keyframes)
{
this->keyframes.push_back(KeyFrame);
}
}
SumOpacity_Channel& SumOpacity_Channel::operator=(const SumOpacity_Channel &opacityChannel)
{
for (auto KeyFrame : keyframes)
{
this->keyframes.push_back(KeyFrame);
}
return *this;
}
void SumOpacity_Channel::AddKeyframe(SumKeyframeFloat key)
{
keyframes.push_back(key);
}
void SumOpacity_Channel::DeleteKeyframe(SumKeyframeFloat key, int number)
{
keyframes.erase(keyframes.begin() + number);
}
SumOpacity_Channel* SumOpacity_Channel::Clone() const
{
return new SumOpacity_Channel(*this);
}
Your channel is empty.
This copy constructor doesn't make any sense:
SumOpacity_Channel::SumOpacity_Channel(const SumOpacity_Channel &opacityChannel)
{
this->keyframes.clear(); // [1]
for (auto KeyFrame : keyframes) // [2]
{
this->keyframes.push_back(KeyFrame);
}
}
in [2] you are iterating over empty vector - it was cleared the line above.
Also why clear is called? It is constructor, so this vector could not be filled ever before. Calling clear makes sense in copy assignment operator, but not here.
So this
cont.AddChannel(&opacityChannel);
with that
void AddChannel(SumOpacity_Channel* channel)
{
Channels.push_back(channel->Clone());
}
SumOpacity_Channel* SumOpacity_Channel::Clone() const
{
return new SumOpacity_Channel(*this);
}
gives empty vector.
auto KeyFrame : keyframes
should be
auto KeyFrame : opacityChannel.keyframes
?

C++ Cereal save/load_and_construct not working

So I'm trying to use the Cereal library and I've come to an issue I can't seem to overcome. Essentially the doc's say it is possible to deserialize Types with no default constructor. Yet in the implementation notes it says Define a serialize or save/load pair as you normally would yet the serialize/load options cannot be defined in a valid manner if there is no default constructor. I take this to mean, the load_and_construct function takes the place of load. Yet when implementing a relatively simple example seen below.
"main.cpp"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <memory>
#include <cereal/access.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/vector.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/archives/portable_binary.hpp>
struct string_wrapper {
const std::string str;
string_wrapper(const std::string& _a) : str{_a} {}
template <class Archive>
void save(Archive& _archive) const {
_archive(str);
}
template <class Archive>
static void load_and_construct(Archive& _archive,
cereal::construct<string_wrapper>& _construct) {
std::string a;
_archive(a);
_construct(a);
}
};
struct wrapper_of_string_wrappers {
const std::vector<string_wrapper> strs;
wrapper_of_string_wrappers(
const std::vector<string_wrapper>& _a
) : strs{_a} { }
template <class Archive>
void save(Archive& _archive) const {
_archive(strs);
}
template <class Archive>
static void load_and_construct(Archive& _archive,
cereal::construct<wrapper_of_string_wrappers>& _construct) {
std::vector<string_wrapper> strs;
_archive(strs);
_construct(strs);
}
};
int main() {
auto file = "test.bin";
{ // save
std::ofstream os(file, std::ios::binary);
cereal::PortableBinaryOutputArchive archiveSave(os);
std::vector<string_wrapper> as;
as.push_back({"Hello"});
as.push_back({"World"});
wrapper_of_string_wrappers test(as);
auto test_ptr = std::make_unique<wrapper_of_string_wrappers>(test);
archiveSave(test_ptr);
}
{ // load
std::ifstream is(file, std::ios::binary);
cereal::PortableBinaryInputArchive archiveLoad(is);
std::unique_ptr<wrapper_of_string_wrappers> test = nullptr;
archiveLoad(test);
std::cout << (*test).strs[0].str << " " << (*test).strs[1].str << std::endl;
}
std::cin.get();
return 0;
}
This code obviously is kind of pointless, its just a minimal example to illustrate the problem I'm running into.
From this page
Non-default constructors are currently only supported for serializing pointers
Your problem here is you are trying to serialize non pointer values with no default constructor here
std::vector<string_wrapper> strs;
_archive(strs);
To solve your problem you need either make default constructor for string_wrapper with save/load pair or use string_wrapper as pointer in wrapper_of_string_wrappers.
Here is working code for second option(string_wrapper remains same):
struct wrapper_of_string_wrappers {
//const std::vector<std::unique_ptr<string_wrapper>> strs;
//const string_wrapper strs;
const std::unique_ptr<string_wrapper> strs;
wrapper_of_string_wrappers(
//const std::vector<std::unique_ptr<string_wrapper>>& _a
const string_wrapper _a
) : strs{ new string_wrapper(_a) } { }
wrapper_of_string_wrappers(
const wrapper_of_string_wrappers& w
) : strs{ new string_wrapper(*w.strs) } { }
template <class Archive>
void save(Archive& _archive) const {
_archive(strs);
}
template <class Archive>
static void load_and_construct(Archive& _archive,
cereal::construct<wrapper_of_string_wrappers>& _construct) {
//std::vector<std::unique_ptr<string_wrapper>> strs;
std::unique_ptr<string_wrapper> strs;
_archive(strs);
_construct(*strs);
}
};
int main() {
auto file = "test.bin";
{ // save
std::ofstream os(file, std::ios::binary);
cereal::PortableBinaryOutputArchive archiveSave(os);
string_wrapper as("Hello");
wrapper_of_string_wrappers test(as);
auto test_ptr = std::make_unique<wrapper_of_string_wrappers>(test);
archiveSave(test_ptr);
}
{ // load
std::ifstream is(file, std::ios::binary);
cereal::PortableBinaryInputArchive archiveLoad(is);
std::unique_ptr<wrapper_of_string_wrappers> test = nullptr;
archiveLoad(test);
std::cout << (*test).strs->str << std::endl;
}
std::cin.get();
return 0;
}

icu::UnicodeString with boost serialize

I am trying to serialize an icu::UnicodeString with the boost serialization library but am having trouble.
The icu::UnicodeString does not have the required serialize function to serialize it. So I tried to create it, but I am not sure how to make these. Example code:
#include <map>
#include <sstream>
#include <fstream>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
namespace boost {
namespace serialization {
template<class Archive>
inline void save(
Archive & ar,
const icu_55::UnicodeString& str,
const unsigned int /* file_version */
){
}
template<class Archive>
inline void load(
Archive & ar,
icu_55::UnicodeString& str,
const unsigned int /* file_version */
){
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<class Archive>
inline void serialize(
Archive & ar,
icu_55::UnicodeString& str,
const unsigned int file_version
){
boost::serialization::split_free(ar, str, file_version);
}
} // serialization
} // namespace boost
int main()
{
std::map<icu::UnicodeString, int> map = {{"asssdasd",2}, {"qwe",1}, {"Zxc",55}};
std::stringstream ss;
boost::archive::text_oarchive oarch(ss);
oarch << map;
std::map<icu::UnicodeString, int> new_map;
boost::archive::text_iarchive iarch(ss);
iarch >> new_map;
std::cout << (map == new_map) << std::endl;
}
Compile with something like g++ -o new new.cpp -std=c++11 -lboost_serialization -licuuc
Currently the "save" and "load" functions are not implemented. I tried doing just the ar & str; statements that are used in the boost manuals, but I am getting a segmentation fault with that that I am also unable to fix.
I've never worked with LibICU directly, so probably someone can review this code.
However, from my experience using Boost Serialization I think this should be helpful:
template <class Archive>
inline void save(Archive &ar, icu_55::UnicodeString const &str, const unsigned int) {
auto sz = str.getCapacity();
auto len = str.length();
auto buf = str.getBuffer();
if (!buf) throw std::invalid_argument("str");
ar & sz & len & boost::serialization::make_array(buf, sz);
}
template <class Archive>
inline void load(Archive &ar, icu_55::UnicodeString &str, const unsigned int)
{
size_t sz, len;
ar & sz & len;
auto buf = str.getBuffer(sz);
if (!buf) throw std::invalid_argument("str");
try {
ar & boost::serialization::make_array(buf, sz);
}
catch(...) {
str.releaseBuffer(len);
throw;
}
str.releaseBuffer(len);
}
It works for the simple test case provided:
#include <fstream>
#include <map>
#include <sstream>
#include <iostream>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/map.hpp>
namespace boost { namespace serialization {
template <class Archive>
inline void save(Archive &ar, icu_55::UnicodeString const &str, const unsigned int) {
auto sz = str.getCapacity();
auto len = str.length();
auto buf = str.getBuffer();
if (!buf) throw std::invalid_argument("str");
ar & sz & len & boost::serialization::make_array(buf, sz);
}
template <class Archive>
inline void load(Archive &ar, icu_55::UnicodeString &str, const unsigned int)
{
size_t sz, len;
ar & sz & len;
auto buf = str.getBuffer(sz);
if (!buf) throw std::invalid_argument("str");
try {
ar & boost::serialization::make_array(buf, sz);
}
catch(...) {
str.releaseBuffer(len);
throw;
}
str.releaseBuffer(len);
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template <class Archive>
inline void serialize(Archive &ar, icu_55::UnicodeString &str, const unsigned int file_version) {
boost::serialization::split_free(ar, str, file_version);
}
} } // serialization // namespace boost
int main() {
std::map<icu::UnicodeString, int> const map = { { "asssdasd", 2 }, { "qwe", 1 }, { "Zxc", 55 } };
std::stringstream ss;
{
boost::archive::text_oarchive oarch(ss);
oarch << map;
}
{
std::map<icu::UnicodeString, int> new_map;
boost::archive::text_iarchive iarch(ss);
iarch >> new_map;
std::cout << (map == new_map) << std::endl;
}
}
Prints
1

Boost serialization, class including pointer

i'm currently working on boost serialization and I came into a moment when I can't really move forward. I have a class Order which holds pointers to Table and Waiter classes. I would like to add just that serialization for all the other classes works perfectly fine, its just order that makes problems. When I try to serialize Order in my main, I get an exemption:
Unhandled exception at 0x77A03FC8 in ConsoleApplication1.exe: Microsoft C++ exception: boost::archive::archive_exception at memory location 0x0081C42C.
and
+ e {m_buffer=0x0081c6f4 "unregistered class - derived class not registered or exported" code=unregistered_class (2) } const boost::archive::archive_exception &
Have you got an idea how to make this connection work properly in serialization call?
Order.h
#pragma once
#include <vector>
#include <string>
#include <iostream>
#include <ctime>
#include "Meal.h"
#include "Table.h"
#include "Waiter.h"
using namespace std;
enum Status { ASSIGNED, PROCESSED, CONFIRMED, SUBMITTED, BEING_PREPARED, READY, SERVED, CANCELED, PAID, FINALIZED };
class Order
{
private:
friend std::ostream & operator<<(std::ostream &os, const Order &tb);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */) {
ar & ID & TimeAndDate & status & table & waiter & meals;
};
protected:
string TimeAndDate;
Status status;
Table * table;
Waiter * waiter;
string ID;
public:
typedef Meal * Meal_Pointer;
list<Meal_Pointer> meals;
Order(string,Table*,Waiter*);
Order():table(new Table),waiter(new Waiter) {};
string CurrentDateAndTime();
void UpdateStatus(Status);
void AssignWaiter(Waiter*);
void AssignTable(Table*);
void AddItem(Meal*);
void append(Meal *_bs)
{
meals.insert(meals.end(), _bs);
}
};
Order.cpp
#include "Order.h"
Order::Order(string id, Table* tab, Waiter* wait)
:ID(move(id)), table(tab), waiter(wait), status(ASSIGNED),
std::ostream & operator<<(std::ostream &os, const Order &tb)
{
//std::list<Meal *>::const_iterator it;
// note: we're displaying the pointer to permit verification
// that duplicated pointers are properly restored.
return os << ' ' << tb.ID << ' ' << tb.TimeAndDate << ' ' << tb.status << ' ' << tb.table << ' ' << tb.waiter << ' ' ;
/*for (it = tb.meals.begin(); it != tb.meals.end(); it++) {
os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
}*/
}
template void Order::serialize<boost::archive::text_iarchive>(
boost::archive::text_iarchive & ar,
const unsigned int file_version
);
template void Order::serialize<boost::archive::text_oarchive>(
boost::archive::text_oarchive & ar,
const unsigned int file_ver
Functions
void save_order(const Order &s, const char * filename) {
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
int main ()
{
filename = "order.txt";
save_order(myOrder, filename.c_str());
Order neworder;
restore_order(neworder, filename.c_str());}
You don't show the relevant code.
The code you do show is messy and broken.
Here I've fixed it enough so it can compile:
Live On Coliru
Obviously, no problem.
// Order.h
//#pragma once
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <ctime>
#include <fstream>
#include <iostream>
using namespace std;
struct Table {};
struct Waiter {};
struct Meal {};
namespace boost { namespace serialization {
template <typename Ar> void serialize(Ar&, Table&, unsigned) { }
template <typename Ar> void serialize(Ar&, Waiter&, unsigned) { }
template <typename Ar> void serialize(Ar&, Meal&, unsigned) { }
} }
enum Status { ASSIGNED, PROCESSED, CONFIRMED, SUBMITTED, BEING_PREPARED, READY, SERVED, CANCELED, PAID, FINALIZED };
class Order {
private:
friend std::ostream &operator<<(std::ostream &os, const Order &tb);
friend class boost::serialization::access;
template <class Archive> void serialize(Archive &ar, const unsigned int /* file_version */) {
ar &ID &TimeAndDate &status &table &waiter &meals;
}
protected:
string TimeAndDate;
string ID;
Table *table;
Waiter *waiter;
Status status;
typedef Meal *Meal_Pointer;
list<Meal_Pointer> meals;
public:
Order(string, Table *, Waiter *);
Order() : table(new Table), waiter(new Waiter){};
string CurrentDateAndTime();
void UpdateStatus(Status);
void AssignWaiter(Waiter *);
void AssignTable(Table *);
void AddItem(Meal *);
void append(Meal *_bs) { meals.insert(meals.end(), _bs); }
};
// Order.cpp
//#include "Order.h"
Order::Order(string id, Table *tab, Waiter *wait) : ID(move(id)), table(tab), waiter(wait), status(ASSIGNED) {}
std::ostream &operator<<(std::ostream &os, const Order &tb) {
return os << ' ' << tb.ID << ' ' << tb.TimeAndDate << ' ' << tb.status << ' ' << tb.table << ' ' << tb.waiter
<< ' ';
/*for (it = tb.meals.begin(); it != tb.meals.end(); it++) {
os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
}*/
}
template void Order::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive &ar, const unsigned int file_version);
template void Order::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive &ar, const unsigned int file_ver);
void save_order(const Order &s, const char *filename) {
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
void restore_order(Order &s, const char *filename) {
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
ia >> s;
}
int main() {
std::string const filename = "order.txt";
{
Order myOrder;
save_order(myOrder, filename.c_str());
}
std::cout << std::ifstream(filename).rdbuf() << "\n";
{
Order neworder;
restore_order(neworder, filename.c_str());
}
}
Hints
What you seem to be doing is serializing polymorphic instances of classes (e.g. derived from Table, Waiter or Meal). In such cases you need to register the derived classes and serialize the base properly:
Common confusions with serializing polymorphic types