无法将std :: string转换为日期类型

时间:2016-10-20 09:33:07

标签: c++ date c++11

我正在使用Howard Hinnant's date library并尝试使用下面显示的代码将std::string转换为date::year_month_day

#include "date.h"
#include <string>
#include <iostream>
#include <sstream>

int main()
{
  std::string d = "2016-11-23";
  std::istringstream in{d};
  date::sys_days sd;
  date::parse(in, "%F", sd);
  date::year_month_day ymd = sd;
  std::cout << ymd << '\n';
}

收到太多错误抱怨‘const class std::time_get<char>’ has no member named ‘get

In file included from convert_string_to_date.cpp:1:0:
date.h: In instantiation of ‘void date::detail::parse(std::basic_istream<_CharT, _Traits>&, const std::basic_string<CharT, Traits>&, date::local_time<Duration>&, std::basic_string<CharT, Traits>*, std::chrono::minutes*) [with CharT = char; Traits = std::char_traits<char>; Duration = std::chrono::duration<int, std::ratio<86400l, 1l> >; date::local_time<Duration> = std::chrono::time_point<date::local_t, std::chrono::duration<int, std::ratio<86400l, 1l> > >; std::chrono::minutes = std::chrono::duration<long int, std::ratio<60l> >]’:
date.h:4371:90:   required from ‘void date::detail::parse(std::basic_istream<_CharT, _Traits>&, const std::basic_string<CharT, Traits>&, date::local_time<Duration>&, std::chrono::minutes*) [with CharT = char; Traits = std::char_traits<char>; Duration = std::chrono::duration<int, std::ratio<86400l, 1l> >; date::local_time<Duration> = std::chrono::time_point<date::local_t, std::chrono::duration<int, std::ratio<86400l, 1l> > >; std::chrono::minutes = std::chrono::duration<long int, std::ratio<60l> >]’
date.h:4447:42:   required from ‘void date::parse(std::basic_istream<_CharT, _Traits>&, const std::basic_string<CharT, Traits>&, date::sys_time<Duration>&) [with CharT = char; Traits = std::char_traits<char>; Duration = std::chrono::duration<int, std::ratio<86400l, 1l> >; date::sys_time<Duration> = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<int, std::ratio<86400l, 1l> > >]’
date.h:4649:59:   required from ‘void date::parse(std::basic_istream<_CharT, _Traits>&, const CharT*, date::sys_time<Duration>&) [with CharT = char; Traits = std::char_traits<char>; Duration = std::chrono::duration<int, std::ratio<86400l, 1l> >; date::sys_time<Duration> = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<int, std::ratio<86400l, 1l> > >]’
convert_string_to_date.cpp:12:27:   required from here
date.h:4192:21: error: ‘const class std::time_get<char>’ has no member named ‘get’
                     f.get(is, 0, is, err, &tm, b, i-1);
                     ^
date.h:4197:25: error: ‘const class std::time_get<char>’ has no member named ‘get’
                         f.get(is, 0, is, err, &tm, ymd, ymd+8);
                         ^
date.h:4211:21: error: ‘const class std::time_get<char>’ has no member named ‘get’
                     f.get(is, 0, is, err, &tm, b, i-1);
                     ^
date.h:4221:25: error: ‘const class std::time_get<char>’ has no member named ‘get’
                         f.get(is, 0, is, err, &tm, hm, hm+6);
                         ^
date.h:4266:25: error: ‘const class std::time_get<char>’ has no member named ‘get’
                         f.get(is, 0, is, err, &tm, hm, hm+2);
                         ^
date.h:4275:21: error: ‘const class std::time_get<char>’ has no member named ‘get’
                     f.get(is, 0, is, err, &tm, b, i-1-modified);
                     ^
date.h:4322:21: error: ‘const class std::time_get<char>’ has no member named ‘get’
                     f.get(is, 0, is, err, &tm, b, i-1);
                     ^
date.h:4343:17: error: ‘const class std::time_get<char>’ has no member named ‘get’
                 f.get(is, 0, is, err, &tm, b, e);

1 个答案:

答案 0 :(得分:7)

gcc 4.9 doesn't have the complete time_get support that date.h requires. It works on gcc 5.