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

时间:2014-03-21 22:36:11

标签: c++ visual-studio-2012 boost

我正在尝试序列化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日期时间来获取当前日期和时间吗?或者我需要在哪里打电话给日期时间的建设者.. ??

由于

1 个答案:

答案 0 :(得分:2)

docs state

  

boost :: date_time库与boost :: serialization库的text和xml存档兼容。可序列化的类列表是:

     
      
  • boost::gregoriandate
      date_durationdate_periodpartial_datenth_day_of_week_in_month,   first_day_of_week_in_month last_day_of_week_in_month,   first_day_of_week_beforefirst_day_of_week_after greg_month,   greg_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
  • 包含posix_time的序列化标题。

程序打印:

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

查看 Live On Coliru