【问题标题】:using from_string with boost date将 from_string 与提升日期一起使用
【发布时间】:2011-08-28 20:43:58
【问题描述】:

我有以下代码:

    #include <iostream>
#include <string>
#include <iomanip>
#include <locale>


#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/gregorian/parsers.hpp>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

using namespace boost::posix_time;
using namespace boost::gregorian;


int main(int argc, char *argv[])
{
 std::string ds("2011-01-02");
date dt(from_string(ds));
date_facet *f=new date_facet("%Y-%m-%d");

 std::locale loc=std::locale(std::locale::classic(),f);
std::cout.imbue(loc);


   std::cout<<dt<<std::endl;


  return 0;

}

当我编译它时,我得到以下错误:

/tmp/ccBWTFcx.o: In function `unsigned short boost::date_time::month_str_to_ushort<boost::gregorian::greg_month>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
   b.cpp:(.text._ZN5boost9date_time19month_str_to_ushortINS_9gregorian10greg_monthEEEtRKSs[unsigned short boost::date_time::month_str_to_ushort<boost::gregorian::greg_month>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x97): undefined reference to `boost::gregorian::greg_month::get_month_map_ptr()'
    collect2: ld returned 1 exit status

这是图书馆链接问题吗?

【问题讨论】:

  • 是的,这是一个库链接问题。您是否链接了 boost 静态库?
  • 不,我没有,我只是做了,并且收到了我在下面列出的错误....

标签: c++ string locale boost-date-time


【解决方案1】:

Boost date_time 不只是header,需要在命令行中链接,例如:

gcc myprogram.cpp -omyprogram -lboost_date_time

【讨论】:

  • 感谢 Autopulated,我做到了,现在我得到这个错误:错误:'locale' 没有在这个范围内声明我该如何解决这个问题?
  • 没有,我刚刚又编译了一遍……你编译的时候有用吗?
  • 它来自您发布的相同代码??未声明的错误来自编译器,它在链接器之前运行,所以如果出现编译器错误,你就不会进入链接阶段——除非它来自不同的文件,也许吧?
  • 你是对的......我需要去给自己喝一杯......我跑错了文件......对不起。谢谢它的工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 2021-08-31
  • 2017-02-08
  • 2021-10-01
  • 2019-12-17
  • 2012-01-05
  • 1970-01-01
相关资源
最近更新 更多