2017-03-23 72 views
1

我正在使用boost::gregorian來執行日期計算。我想用add_month具體根據例子(電流的1.63 http://www.boost.org/doc/libs/1_63_0/doc/html/date_time/examples.htmladd_month從boost :: gregorian中刪除?

/* Simple program that uses the gregorian calendar to progress by exactly 
* one month, irregardless of how many days are in that month. 
* 
* This method can be used as an alternative to iterators 
*/ 
#include "boost/date_time/gregorian/gregorian.hpp" 
#include <iostream> 

int main() 
{ 

    using namespace boost::gregorian; 

    date d = day_clock::local_day(); 
    add_month mf(1); 
    date d2 = d + mf.get_offset(d); 
    std::cout << "Today is: " << to_simple_string(d) << ".\n" 
    << "One month from today will be: " << to_simple_string(d2) 
    << std::endl; 

    return 0; 
} 

然而,這提供錯誤消息

month.cpp: In function `int main()':         
month.cpp:33:5: error: `add_month' was not declared in this scope  
add_month mf(1);             
^                 
month.cpp:35:19: error: `mf' was not declared in this scope    
date d2 = d + mf.get_offset(d);         
      ^             
+0

錯誤報告:https://svn.boost.org/trac/boost/ticket/ 10627 – NathanOliver

+0

您可以在''/ your-installation-path/libs/date_time/example/gregorian /' –

回答

1

事實上。這個例子已經過時了。事實上,我不記得看到這個功能,所以它可能會過時。

我推薦以下方法代替:

/* Simple program that uses the gregorian calendar to progress by exactly 
* one month, irregardless of how many days are in that month. 
* 
* This method can be used as an alternative to iterators 
*/ 

#include "boost/date_time/gregorian/gregorian.hpp" 
#include "boost/date_time/gregorian/gregorian.hpp" 
#include <iostream> 

int main() 
{ 

    using namespace boost::gregorian; 

    date d = day_clock::local_day(), 
     prev = d - months(1), 
     next = d + months(1); 

    std::cout << "Today is: "      << to_simple_string(d) << ".\n" 
       << "One month before today was: " << to_simple_string(prev) << "\n" 
       << "One month from today will be: " << to_simple_string(next) << "\n"; 
} 

打印了(對我來說):

Today is: 2017-Mar-23. 
One month before today was: 2017-Feb-23 
One month from today will be: 2017-Apr-23 
+0

提供您的boost軟件包中的最新示例文件(這些文件可以更新,因爲它很容易運行)考慮給圖書館維護人員單挑 – sehe

+2

謝謝你的擡頭。 –

+0

謝謝@sehe,我已經做出了改變,它運作良好。 – tompdavis

相關問題