2017-05-28 18 views
0

私はC++を初めて使用しています。オブジェクトをバイナリでシリアル化して、mysqlサーバーに保存してデシリアライズする方法の詳細な例は見つかりません。C++でオブジェクトをシリアライズおよび逆シリアル化する

オブジェクトは、次のようになります。

class house{ 
     public: 
      int houseNumber; 
      bool isEmpty; 
      Dweller *dweller; 
}; 
    class Dweller{ 
     public: 
     int phoneNumber; 
     bool man; 
     Backpack backpack; 
}; 
    class Backpack{ 
     public: 
     int item1; 
     int item2; 
     int item3; 
     int item4; 
     float weight; 
     bool empty; 
}; 

私は他のオブジェクトを所有する家屋オブジェクトを、シリアライズする必要があります。

+0

どのような種類のバイナリ形式ですか? protobufはオプションですか? https://developers.google.com/protocol-buffers/docs/overview – Anton

+0

protobufはオプションになると思います –

答えて

0

私が使用して問題を解決することができました:

#include <boost/serialization/serialization.hpp> 
#include <boost/serialization/vector.hpp> 

は今、「住人」は

物事を容易にするために、ベクターに続いて、コードは次のように滞在しているので、私は#include "boost/serialization/vector.hpp"を使用を参照してください。

class house{ 
    public: 
     int houseNumber; 
     bool isEmpty; 
     std::vector<Dweller> dweller; 
    private: 
     friend class boost::serialization::access; 
     template <class archive> 
     void serialize(archive &ar, const unsigned int version) 
     { 
     ar &houseNumber; 
     ar &isEmpty; 
     ar &dweller; 
     } 
}; 
class Dweller{ 
    public: 
     int phoneNumber; 
     bool man; 
     Backpack backpack; 
    private: 
     friend class boost::serialization::access; 
     template <class archive> 
     void serialize(archive &ar, const unsigned int version) 
     { 
     ar &phoneNumber; 
     ar &man; 
     ar &backpack; 
     } 
}; 
class Backpack{ 
    public: 
     int item1; 
     int item2; 
     int item3; 
     int item4; 
     float weight; 
     bool empty; 
    private: 
     friend class boost::serialization::access; 
     template <class archive> 
     void serialize(archive &ar, const unsigned int version) 
     { 
     ar &item1; 
     ar &item2; 
     ar &item3; 
     ar &item4; 
     ar &weight; 
     ar &empty; 
     } 
}; 

だから私は使用シリアライズします

#include <iostream> 
#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 
#include <boost/serialization/serialization.hpp> 
#include <sstream> 

int main() 
{ 
    Backpack _backpack; 
    _backpack.item1 = 0; 
    _backpack.item1 = 1; 
    _backpack.item1 = 2; 
    _backpack.item1 = 3; 
    _backpack.item1 = 4; 
    _backpack.weight = 3.21; 
    _backpack.empty = false; 
    Dweller _dweller1; 
    _dweller.phoneNumber = 1234567; 
    _dweller.man = false; 
    _dweller.backpack = _backpack; 
    Dweller _dweller2; 
    _dweller.phoneNumber = 7654321; 
    _dweller.man = true; 
    _dweller.backpack = _backpack; 

    std::vector<Dweller> _dwellers; 
    _dwellers.push_back(_dweller1); 
    _dwellers.push_back(_dweller2); 
    house _house; 
    _house.houseNumber = 4; 
    _house.isEmpty = false; 
    _house.dweller = _dwellers; 

    /*Serializing*/ 
    std::stringstream ss; 
    boost::archive::binary_oarchive oa(ss); 
    oa << _house; 
    /*Deserializing*/ 
    boost::archive::binary_iarchive ia(ss); 
    house _house2; 
    ia >> _house2; 
} 
関連する問題