2011-12-10 3 views
0

부스트 직렬화의 기본 사항을 배우려고합니다. 그래서 나는 the tutorial을 따라 갔고 A a_;B b_;을 개인 회원으로 갖는 간단한 class Aclass Bclass C을 만들었습니다. IDEone에서 직렬화 가능한 중첩 클래스가있는 클래스의 직렬화 이상한 컴파일 오류

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

class A{ 
private: 
     friend class boost::serialization::access; 

     template<class Archive> 
     void serialize(Archive & ar, const unsigned int version) 
     { 
       ar & a_; 
     } 

     int a_; 

public: 
     A(){ std::cout << "A constructed" << std::endl; } 
     A(int a): a_(a) { std::cout << "A constructed with 'a' ==" << a << std::endl; } 
}; 
class B{ 
private: 
     template<class Archive> 
     void serialize(Archive & ar, const unsigned int version) 
     { 
       ar & b_; 
     } 

     std::string b_; 
public: 
     B(){ std::cout << "B constructed" << std::endl; } 
     B(std::string b): b_(b) { std::cout << "B constructed with 'b' ==" << b << std::endl; } 
}; 

class C{ 
private: 
     template<class Archive> 
     void serialize(Archive & ar, const unsigned int version) 
     { 
       ar & a_; 
       ar & b_; 
       ar & d_; 
     } 

     A a_; 
     B b_; 
     double d_; 

public: 
     C(){ std::cout << "C constructed" << std::endl; } 
     C(int a, std::string b, double d): a_(a), b_(b), d_(d) { std::cout << "C constructed with 'd' == " << d << std::endl; } 
}; 

int main() { 
    // create and open a character archive for output 
    std::ofstream ofs("filename"); 

    // create class instance 
    C c(15, "rock and roll", 25.8); 

    // save data to archive 
    { 
      boost::archive::text_oarchive oa(ofs); 
      // write class instance to archive 
      oa << c; 
      // archive and stream closed when destructors are called 
    } 

    C c_recreated; 
    { 
      // create and open an archive for input 
      std::ifstream ifs("filename"); 
      boost::archive::text_iarchive ia(ifs); 
      // read class state from archive 
      ia >> c_recreated; 
      // archive and stream closed when destructors are called 
    } 

    std::cin.get(); 
} 

는 모든 이상하고 무서운 컴파일러 오류가 here입니다 살고 있습니다. 내 VS2010에 나는 단지이 동일한 오류를 가지고 있지만 :

Error 2 error C2248: 'C::serialize' : cannot access private member declared in class 'C' 
Error 3 error C2248: 'C::serialize' : cannot access private member declared in class 'C'  
내가 class Aclass B을 가진 후 class C 직렬화 할 수있는 방법 내가 잘못했을 무엇

,?을

답변

3

BC에 대해 friend class boost::serialization::access;을 입력하지 않았습니다.

+0

[여기] (http://ideone.com/2N1f3)을 추가 한 후에도 여전히 IDEone에서 컴파일되지 않습니다. 이유는이 코드가 Linux에서 컴파일되지 않는다는 것입니다. IDEone 컴파일을 위해이 코드를 수정하는 방법은 무엇입니까? 내 VS에 완벽하게 컴파일, 정말 고마워요 !!!))) – myWallJSON

+1

@ myWallJSON : Ideone과 같은 것 같아 부스트의 헤더를 제공하고, Boost.Serialize 같은 것들에 대한 컴파일 된 라이브러리가 없습니다. 이것이 링커가 불평하는 이유입니다. – Xeo