2015-01-30 6 views
1

나는 boost :: serialize 라이브러리를 사용하여 객체를 직렬화 및 비 직렬화하려고 시도합니다. 내 저장 및로드 기능을 분할해야합니다.boost :: serialize (SSCCE 사용)로 여러 값을 deserialize

라이브러리를 사용하는 것은 공식 자습서에 설명되어 있습니다. 내 저장 및로드 함수는 다음과 같습니다.

friend class boost::serialization::access; 

template<typename Archive> 
void save(Archive& ar, const unsigned version) const { 
    ar & name; 
    ar & NType; 
    ar & NTherm; 
    ar & NRun; 
//... 
} 

template<class Archive> 
void load(Archive& ar, const unsigned int version) { 
    ar & name; 
    ar & NType; 
    ar & NTherm; 
    ar & NRun; 
//... 
} 
BOOST_SERIALIZATION_SPLIT_MEMBER() 

이러한 함수는 클래스의 헤더에 구현됩니다. 나는 다음과 같이 serialize하고 deserialize한다. { // Serialize std :: of ofs ("output.txt"); boost :: archive :: text_oarchive oa (ofs); oa < < 개체; }

{ 
    //Deserialize 
    Class newObject; 
    std::ifstream ifs("output.txt"); 
    boost::archive::text_iarchive ia(ifs); 
    ia >> newObject; 
} 

직렬화는 잘 작동하지만, 잠시 직렬화는 ar & NRun;에서 예외가 발생합니다.

다음과 같은 오류 메시지가 나타납니다. 이 응용 프로그램은 비정상적인 방식으로 종료하도록 런타임을 요청했습니다. 디버깅에서 클래스 이름이 너무 길어서 예외가 발생했음을 나타냅니다.

어떻게 해결할 수 있습니까?

업데이트 : 코드 스 니펫에 대괄호가 추가되었습니다.

업데이트 2 : SSCCE를 추가했습니다.

MAIN.CPP :

#include <iostream> 
#include "simulation.h" 
#include <fstream> 
#include <boost/archive/text_iarchive.hpp> 
#include <boost/archive/text_oarchive.hpp> 
#include <boost/serialization/split_member.hpp> 


int main() 
{ 
    Simulation *sim; 
    sim = new Simulation(2,25,25,25,100,500,1000,"Sim"); 
    { 
     std::ofstream ofs("output.txt"); 
     boost::archive::text_oarchive oa(ofs); 
     oa << sim; 
    } 

    { 
     Simulation newSim; 
     std::ifstream ifs("output.txt",std::ios::binary); 
     boost::archive::text_iarchive ia(ifs); 
     ia >> newSim; 
    } 
} 

simulation.h :

#ifndef SIMULATION_H_ 
#define SIMULATION_H_ 
#include <string> 
#include <boost/serialization/access.hpp> 
#include <boost/serialization/split_member.hpp> 
#include <boost/serialization/string.hpp> 

class Simulation { 

public: 
    //Constructors 
Simulation(int anzType, int x=25, int y=25, int z=25, int NT = 100, int NS = 500, int NR = 1000, std::string n = ""); 
Simulation();   //Defaultconstructor für Boost Serialisierung 

    //Destructor 
    virtual ~Simulation(); 


private: 
    int NType;    
    int NTherm;    
    int NStep;    
    int NRun;    
    std::string name; 
    int Lx; 
    int Ly; 
    int Lz; 
    int LyLz; 

    friend class boost::serialization::access; 

    template<typename Archive> 
    void save(Archive& ar, const unsigned version) const { 
     ar & name; 
     ar & NType; 
     ar & NTherm; 
     ar & NRun; 
     ar & NStep; 
    } 

    template<class Archive> 
    void load(Archive& ar, const unsigned int version) { 
     ar & name; 
     ar & NType; 
     ar & NTherm; 
     ar & NRun; 
     ar & NStep; 
    } 
    BOOST_SERIALIZATION_SPLIT_MEMBER() 
}; 
#endif /* SIMULATION_H_ */ 

simulation.cpp :

#include "Simulation.h" 


Simulation::Simulation() { 

} 


Simulation::Simulation(int anzType, int x, int y, int z, int NT, int NS, int NR, std::string n) { 
    name = n; 
    NType = anzType; 
    NTherm = NT; 
    NStep = NS; 
    NRun = NR; 
    Lx = x; 
    Ly = y; 
    Lz = z; 
    LyLz = y*z; 
} 

Simulation::~Simulation() { 

} 

답변

2

UPDATE 질문을 SSCCE로 업데이트 한 것이 확실합니다.

Simulation*을 직렬화합니다. 그런 다음를 비 직렬화하려고합니다. 당연히이 방법은 효과가 없습니다.

Live On Coliru

#include <boost/archive/text_iarchive.hpp> 
#include <boost/archive/text_oarchive.hpp> 
#include <boost/serialization/access.hpp> 
#include <fstream> 
#include <iostream> 

class Simulation { 

    public: 
    // Constructors 
    Simulation(int anzType, int x = 25, int y = 25, int z = 25, int NT = 100, int NS = 500, int NR = 1000, std::string n = ""); 
    Simulation(); // Defaultconstructor für Boost Serialisierung 

    // Destructor 
    virtual ~Simulation(); 

    private: 
    std::string name; 
    int   NType, NTherm, NStep, NRun; 
    int   Lx, Ly, Lz, LyLz; 

    friend class boost::serialization::access; 

    template <typename Archive> void serialize(Archive &ar, unsigned) { 
     ar & name; 
     ar & NType; 
     ar & NTherm; 
     ar & NRun; 
     ar & NStep; 
    } 
}; 

Simulation::Simulation() {} 

Simulation::Simulation(int anzType, int x, int y, int z, int NT, int NS, int NR, std::string n) 
    : name(n), NType(anzType), NTherm(NT), NStep(NS), NRun(NR), 
    Lx(x), Ly(y), Lz(z), LyLz(y * z) 
{ 
} 

Simulation::~Simulation() {} 

int main() { 
    Simulation *sim = new Simulation(2, 25, 25, 25, 100, 500, 1000, "Sim"); 
    { 
     std::ofstream ofs("output.txt", std::ios::binary); 
     boost::archive::text_oarchive oa(ofs); 
     oa << sim; 
    } 

    { 
     Simulation* newSim = nullptr; 
     std::ifstream ifs("output.txt", std::ios::binary); 
     boost::archive::text_iarchive ia(ifs); 
     ia >> newSim; 

     delete newSim; 
    } 
} 
+0

Neather'표준 : IOS :: binary'도 명시 적 홍조는 문제를 해결한다. 스트림은 프로그램에서 별도의 기능으로 사용됩니다. – pyman

+0

그러면 SSCCE로 만들어야합니다. 코드는 괜찮습니다. 문제가 다른 곳에서 발생했습니다 – sehe

+0

방금 ​​programm이 다음과 같은 오류를 출력하는 것으로 보았습니다 :'C 런타임 함수에 전달 된 매개 변수가 잘못되었습니다. ' – pyman