2011-08-28 34 views
7

Tôi có đoạn mã sau:sử dụng from_string với tăng ngày

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

} 

và khi tôi biên dịch nó, tôi nhận được lỗi sau:

/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 

đây là vấn đề liên kết thư viện?

+0

Vâng, đó là một vấn đề liên kết thư viện. Bạn đã liên kết chống lại các lib tĩnh tăng chưa? –

+0

nope, tôi đã không, tôi chỉ làm và nhận được lỗi tôi đã liệt kê dưới đây .... – itcplpl

Trả lời

8

Boost DATE_TIME không được chỉ tiêu đề, bạn cần phải liên kết nó trên dòng lệnh, ví dụ:

gcc myprogram.cpp -omyprogram -lboost_date_time 
+0

cảm ơn Autopulated, tôi đã làm điều đó, bây giờ tôi nhận được lỗi này: lỗi: 'locale' không được khai báo trong phạm vi này làm cách nào để khắc phục điều đó? – itcplpl

+0

@itcplpl Bạn đã thay đổi mã chưa? – James

+0

không, tôi vừa biên dịch lại ... nó có hoạt động khi bạn biên dịch không? – itcplpl

Các vấn đề liên quan