在Boost序列化中,Boost::local_time::local_date_time给出错误

In Boost serialization, boost::local_time::local_date_time gives error

本文关键字:time local Boost 出错 错误 date 序列化      更新时间:2023-10-16

我正在尝试序列化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(MyData(;BOOST_CLASS_Elementation(MyData,BOOST::serialization::object_serializable(;BOOST_CLASS_TRACKING(MyData,BOOST::serialization::track_selectly(;

请在这个话题上提供帮助,投入更多的时间,但到目前为止没有用。

我可以使用posix日期时间来获取当前日期和时间吗??或者我需要在哪里打电话给解释者询问日期时间??

感谢

文档状态:

boost::date_time库与boost::序列化库的文本和xml存档兼容。可序列化的类列表为:

  • boost::gregoriandate
    date_durationdate_periodpartial_datenth_day_of_week_in_month,CCD_ 6 CCD_,first_day_of_week_beforefirst_day_of_week_aftergreg_monthgreg_daygreg_weekday
  • boost::posix_time
    ptimetime_durationtime_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
  • 包括CCD_ 20的序列化报头

程序打印:

22 serialization::archive 10 0 0 0  0  0 0 0 0 15 not-a-date-time

查看Coliru直播

相关文章: