我正在尝试序列化MyData,但boost :: local_time :: local_date_time给出错误: “错误1错误C2512:'boost :: local_time :: local_date_time_base<>' :没有合适的默认构造函数“
以下是代码:
// MyData.hpp文件
struct MyData
{
std::string id;
std::string name;
boost::local_time::local_date_time time;
private:
friend class boost::serialization::access;
template
void serialize(Archive &ar, const unsigned int version)
{
ar & id;
ar & name;
ar & time; // when i comment this line, error goes off
}
public:
MyData(void);
MyData(const parameter_strings & parms);
virtual ~MyData(void);
};
}
// MyData.cpp file
MyData::MyData(void)
{
}
MyData::~MyData(void)
{
}
MyData::MyData(const parameter_strings & parms)
{
// implementation aprt
}
BOOST_CLASS_EXPORT_IMPLEMENT(迈德特); BOOST_CLASS_IMPLEMENTATION(迈德特,升压::系列化:: object_serializable); BOOST_CLASS_TRACKING(迈德特,升压::系列化:: track_selectively);
请帮助解决这个问题,投入更多时间,但到现在为止没有用。
我可以使用posix日期时间来获取当前日期和时间吗?或者我需要在哪里打电话给日期时间的建设者.. ??
由于
答案 0 :(得分:2)
boost :: date_time库与boost :: serialization库的text和xml存档兼容。可序列化的类列表是:
boost::gregoriandate
date_duration
,date_period
,partial_date
,nth_day_of_week_in_month
,first_day_of_week_in_month
last_day_of_week_in_month
,first_day_of_week_before
,first_day_of_week_after
greg_month
,greg_day
,greg_weekday
boost::posix_time
ptime
,time_duration
,time_period
所以,是的。但您应该使用ptime
而不是local_date_time。
现在,首先,编译器抱怨它不知道如何初始化time
成员(因为它没有默认构造函数)。这与序列化无关:
struct Oops
{
boost::local_time::local_date_time time;
Oops() { }
};
已经存在同样的问题。解决它:
struct FixedOops
{
boost::local_time::local_date_time time;
FixedOops() : time(boost::local_time::not_a_date_time)
{
}
};
现在,要序列化:
#include <boost/archive/text_oarchive.hpp>
#include <boost/date_time/posix_time/time_serialize.hpp>
#include <boost/date_time/local_time/local_time.hpp>
struct parameter_strings {};
struct MyData
{
std::string id;
std::string name;
boost::posix_time::ptime time;
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & id;
ar & name;
ar & time; // when i comment this line, error goes off
}
public:
MyData() : time(boost::posix_time::not_a_date_time) { }
MyData(parameter_strings const&) : time(boost::posix_time::not_a_date_time) { }
virtual ~MyData() { };
};
int main()
{
boost::archive::text_oarchive oa(std::cout);
MyData data;
oa << data;
}
那就是
ptime
posix_time
的序列化标题。程序打印:
22 serialization::archive 10 0 0 0 0 0 0 0 0 15 not-a-date-time