Tôi đã đọc qua vô số câu hỏi tương tự nhưng chưa tìm thấy câu trả lời. Tôi đang sử dụng Visual Studio 2010 và tăng 1,47.tăng tuần tự hóa đối tượng dẫn xuất không gọi là serialize()
Dưới đây là các mã, đó là đầy đủ và compilable:
#include "stdafx.h"
#include <string>
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
using namespace std;
class BaseObject
{
public:
BaseObject(void) { };
virtual ~BaseObject(void) { };
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{ /* nothing happens here */ };
};
class DerivedObject : public BaseObject
{
public:
string text;
public:
DerivedObject(void) { };
~DerivedObject(void) { };
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & text;
};
};
BOOST_CLASS_EXPORT(DerivedObject)
int _tmain(int argc, _TCHAR* argv[])
{
DerivedObject der;
der.text = "Testing!";
std::ostringstream os;
boost::archive::text_oarchive oa(os);
oa.register_type<DerivedObject>();
// I made a DerivedObject, but I'm casting it to a BaseObject
// as the serialization code should not have to know what type it is
BaseObject *base = &der;
// now serialize it
oa << *base;
printf("serialized: %s\r\n",os.str().c_str());
return (0);
}
Bạn có thể thấy nó thật đơn giản, và tôi đã thêm BOOST_CLASS_EXPORT và oa.register_type kỳ diệu đó là nghĩa vụ để đảm bảo DerivdObject :: serialize() được gọi là mặc dù nó không phải là một phương pháp ảo .. nhưng vẫn chỉ serialize() trong BaseObject được gọi. Một vấn đề cụ thể cho Visual C++ có lẽ? Làm ơn cho lời khuyên?
Không phải 'BaseObject :: serialize' của bạn được đánh dấu là' virtual'? – Nick
Không có mẫu nào, nó có thể t –
Điểm tốt - không nhận thấy mẫu bit! – Nick