0
我想了解一些boost序列化的基礎知識。所以我跟着the tutorial創建了簡單的class A
和class B
和class C
,它有A a_;
和B b_;
作爲私人成員。具有序列化嵌套類的類的序列化奇怪的編譯錯誤
#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();
}
在IDEone生活是here所有的陌生和可怕的編譯器錯誤。雖然在我的VS2010我只拿到了2個相同的錯誤:
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 C
序列化有class A
和class B
後?
在添加[here](http://ideone.com/2N1f3)之後它仍然不能在IDEone上編譯,爲什麼,這是否意味着這些代碼不會在linux上編譯,如何修復它以便IDEone編譯?編譯完全在我的VS上,非常感謝你!!!))) – myWallJSON
@myWallJSON:好像Ideone只提供了Boost的頭文件,並沒有像Boost.Serialize這樣的東西的編譯庫。這就是連接器抱怨的原因。 – Xeo