Boost serialization

Pages: 1234
Those first errors should be obvious. You've declared Audi::hp private, so of course it can't be accessed from outside the class.
Of course, but I'd prefer to leave them private. I was wondering if there is a way with the constructor to call them. Probably not but maybe there is a possibility.
What do you mean by "call them"? You don't call a variable, you call a function.

Obviously, the class's constructor can access those variables.

And... if you're playing around with advanced features like templates, polymorphism and Boost libraries, you must surely have come across getter and setter methods before?
Last edited on
Hey everyone, I'm getting the error "std::bad_alloc" when running the code below. I've debugged the problem and the culprit is line 140:

 
std::stringstream strs(str);


I commented out lines 136-148 (the loading part of the code) and the code compiled without problem (no std::bad_alloc as well). Does anyone have an idea what that problem could be?

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <armadillo>

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/unique_ptr.hpp>

#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <vector>

//Serialization for Armadillo Matrices------------------
BOOST_SERIALIZATION_SPLIT_FREE(arma::mat)
namespace boost::serialization {
    template<class Archive>
    void save(Archive & ar, const arma::mat &t, unsigned int version) {
        ar << t.n_elem;
        auto data = t.colptr(0);
        for (size_t K = 0; K < t.n_elem; ++K)
            ar << data[K];
    }

    template<class Archive>
    void load(Archive & ar, arma::mat &t, unsigned int version) {
        size_t n_elem;
        ar >> n_elem;
        t.set_size(n_elem);
        t.zeros();
        auto data = t.colptr(0);
        for (size_t K = 0; K < n_elem; ++K)
            ar >> data[K];
    }
}//-------------------------------------------------------

class Car {
public:
    //Car() = default;
    //virtual double getInfo() = 0;
    virtual char const* type() const = 0;
    virtual ~Car() = default;

private:
    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive& ar, const unsigned int version) {};
};

class Porsche : public Car {
public:
    char const* type() const override { return "Porsche"; }
    //double getInfo() override { return 1.1; }
    Porsche(std::string owner1, int hp1, arma::mat A1)
        {
        owner = owner1;
        hp = hp1;
        A = A1; 

        }
    std::string owner;
    int hp{};
    arma::mat A;
    Porsche() = default;
private:
    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & boost::serialization::base_object<Car>(*this); //https://theboostcpplibraries.com/boost.serialization-class-hierarchies
        ar & owner;
        ar & hp;
        ar & A;
    }    

    
};

class Audi : public Car {
public:
    char const* type() const override { return "Audi"; }
    //double getInfo() override { return 2.2; }
    Audi(std::string owner1, int hp1, std::string second_owner1, std::string country1,  arma::mat A1)
        {
        owner = owner1;
        hp = hp1;
        second_owner = second_owner1; 
        country = country1;
        A = A1; 
        }
    Audi() = default;

    std::string owner;
    int hp{};
    std::string second_owner;
    std::string country;
    arma::mat A;

private:
    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & boost::serialization::base_object<Car>(*this); //https://theboostcpplibraries.com/boost.serialization-class-hierarchies
        ar & owner;
        ar & hp;
        ar & second_owner;
        ar & country;
        ar & A;
    }

};

BOOST_CLASS_EXPORT(Audi);
BOOST_CLASS_EXPORT(Porsche);
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Car); //Tell Boost that Car is abstract



int main() {
    std::string str;
   {
        std::unique_ptr<Car> audi = std::make_unique<Audi>("Wilma", 3, "Rene", "Argentina" ,arma::randu<arma::mat>(4,5));

        std::unique_ptr<Car> porsche = std::make_unique<Porsche>("Joe", 14, arma::randu<arma::mat>(6,5));

        std::stringstream strs;
        boost::archive::binary_oarchive ar(strs);
        ar& audi;
        ar& porsche;

        str = strs.str();
    }

    {
        std::unique_ptr<Audi> audi = std::make_unique<Audi>();
        std::unique_ptr<Porsche>porsche = std::make_unique<Porsche>();

        std::stringstream strs(str);
        boost::archive::binary_iarchive ar(strs);
        ar& audi;
        ar& porsche;

        std::cout << "audi: hp=" << audi->hp << "\n";
        std::cout << "porsche: hp=" << porsche->hp<< " owner=" << porsche->owner << " A=" << porsche->A <<"\n";

    }
    
}
Last edited on
Topic archived. No new replies allowed.
Pages: 1234