Bonjour, je suis actuellement en train de réaliser une classe représentant une carte en deux dimensions et je bloque à l'étape de la rendre serialisable.
En effet, l'écriture dans le fichier marche mais le chargement un peu moins bien. Voici le code source de ladite classe :
Et le main.cpp pour charger le fichier :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef MAP2D_HPP #define MAP2D_HPP #include <stdexcept> #include <boost/multi_array.hpp> #include <boost/serialization/serialization.hpp> #include <boost/serialization/vector.hpp> /** A class which represents a 2D map \fixme boost::mutli_array is not serializable **/ template<class T> class Map2D { public: /** Default constructor \param width Width of the map \param height Height of the map **/ Map2D(size_t width, size_t height) : m_data(boost::extents[width][height]) {} Map2D() : m_data(boost::extents[0][0]) {} /** Default destructor **/ virtual ~Map2D() {} /** Return the map width \return Width of the map **/ size_t width() const { return m_data.shape()[0]; } /** Return the map height \return Height of the map **/ size_t height() const { return m_data.shape()[1]; } /** Return an object in the map \param x X position of the object \param y Y position of the object \return The object in pos x,y **/ T& at(size_t x, size_t y) { if(x < width() && y < height()) return m_data[x][y]; else throw std::out_of_range("Out of limit Map2D"); } void set(size_t x, size_t y, T value) { m_data[x][y] = value; } private: // attrbs boost::multi_array<T, 2> m_data; // serialization friend class boost::serialization::access; template<class Archive> void save(Archive& ar, const unsigned int version) const { // convert the boost::multi_array into vectors std::vector<std::vector<T> > out; for(size_t x=0; x<width(); x++) { std::vector<T> line; for(size_t y=0; y<height(); y++) line.push_back(m_data[x][y]); out.push_back(line); } // serialize the big vector ar & out; } template<class Archive> void load(Archive& ar, const unsigned int version) { std::vector<std::vector<T> > in; // read the vector ar & in; // convert the vector into a boost::multi_array m_data.resize(boost::extents(in.size(), in[0].size())); for(size_t x=0; x<in.size(); x++) { for(size_t y=0; y<in[x].size(); y++) m_data[x][y] = in[x][y]; } } BOOST_SERIALIZATION_SPLIT_MEMBER() // serialization constructor }; #endif
Cependant le compilo me crache dessus :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <fstream> #include <boost/archive/text_iarchive.hpp> #include "Map2D.hpp" int main() { std::ifstream ofs("out.dat"); Map2D<bool> map; { boost::archive::text_iarchive oa(ofs); oa >> map; } return 0; }
Auriez-vous des idées ? Car personnellement j'ai du mal à interpreter ce message d'erreur...In file included from ../src/main.cpp:4:0:
../src/Map2D.hpp: In member function ‘void Map2D<T>::load(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive, T = bool]’:
/usr/include/boost/serialization/access.hpp:101:9: instantiated from ‘static void boost::serialization::access::member_load(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = Map2D<bool>]’
/usr/include/boost/serialization/split_member.hpp:54:13: instantiated from ‘static void boost::serialization::detail::member_loader<Archive, T>::invoke(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = Map2D<bool>]’
/usr/include/boost/serialization/split_member.hpp:69:5: instantiated from ‘void boost::serialization::split_member(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = Map2D<bool>]’
../src/Map2D.hpp:99:3: instantiated from ‘void Map2D<T>::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive, T = bool]’
/usr/include/boost/serialization/access.hpp:118:9: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = Map2D<bool>]’
/usr/include/boost/serialization/serialization.hpp:69:5: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = Map2D<bool>]’
/usr/include/boost/serialization/serialization.hpp:128:9: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = Map2D<bool>]’
/usr/include/boost/archive/detail/iserializer.hpp:188:5: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive, T = Map2D<bool>]’
../src/main.cpp:18:1: instantiated from here
../src/Map2D.hpp:91:4: erreur: no match for call to ‘(boost::multi_array_types::extent_gen) (std::vector<std::vector<bool, std::allocator<bool> >, std::allocator<std::vector<bool, std::allocator<bool> > > >::size_type, std::vector<bool, std::allocator<bool> >::size_type)’
Merci d'avance
EDIT : Résolu, mauvaise utilisation de boost::extents
Partager