简体   繁体   English

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

[英]In Boost serialization, boost::local_time::local_date_time gives error

I'm trying to serialize MyData but boost::local_time::local_date_time gives error : "Error 1 error C2512: 'boost::local_time::local_date_time_base<>' : no appropriate default constructor available" 我正在尝试序列化MyData,但boost :: local_time :: local_date_time给出了错误: “错误1错误C2512:'boost :: local_time :: local_date_time_base <>':没有适当的默认构造函数可用”

Below is the code : 下面是代码:

// MyData.hpp file // 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_EXPORT_IMPLEMENT(MyData); BOOST_CLASS_IMPLEMENTATION(MyData,boost::serialization::object_serializable); BOOST_CLASS_IMPLEMENTATION(MyData,boost :: serialization :: object_serializable); BOOST_CLASS_TRACKING(MyData,boost::serialization::track_selectively); BOOST_CLASS_TRACKING(MyData,boost :: serialization :: track_selectively);

Please help on this topic, investing more time but till now no use. 请为这个主题提供帮助,投入更多时间,但到目前为止没有任何用处。

Can i use posix date time to get current date and time ?? 我可以使用posix日期时间获取当前日期和时间吗? or where do i need to call the construtor for date-time.. ?? 或我需要在哪里打电话给建设者约会。

Thanks 谢谢

The docs state : 文档状态

The boost::date_time library is compatible with the boost::serialization library's text and xml archives. boost :: date_time库与boost :: serialization库的文本和xml存档兼容。 The list of classes that are serializable are: 可序列化的类的列表为:

  • 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 date_durationdate_periodpartial_datenth_day_of_week_in_monthfirst_day_of_week_in_month last_day_of_week_in_monthfirst_day_of_week_beforefirst_day_of_week_after greg_monthgreg_daygreg_weekday
  • boost::posix_time
    ptime , time_duration , time_period ptimetime_durationtime_period

So, yes. 所以,是的。 But you should use ptime instead of local_date_time. 但是您应该使用ptime而不是local_date_time。

Now, first things first, the compiler is complaining that it doesn't know how to initialize the time member (since it has no default constructor). 现在,首先,编译器抱怨它不知道如何初始化time成员(因为它没有默认构造函数)。 This has nothing to do with serialization: 这与序列化无关:

struct Oops
{
    boost::local_time::local_date_time time; 
    Oops() { }
};

has the same problem already. 已经有相同的问题。 Fix it: 修理它:

struct FixedOops
{
    boost::local_time::local_date_time time; 
    FixedOops() : time(boost::local_time::not_a_date_time) 
    { 
    }
};

Now, to serializing: 现在,要序列化:

#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;
}

So that was 所以那是

  • change to ptime 更改为ptime
  • include the serialization header for posix_time . 包括posix_time的序列化头。

The program prints: 程序打印:

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

See it Live On Coliru 在Coliru上实时观看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM