[英]Boost date time conversion error
我從synaptic安裝了boost。
現在我需要將日期從/轉換為字符串,但是當我編寫如下代碼時,
date dt{2018-9-14};
string str=to_simple_string(dt);
cout<<str<<endl;
它給出了一個錯誤:
/usr/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/usr/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
我怎么解決這個???
正如另一個答案所說,構造函數是錯誤的(使用逗號,或者你只是說2018-9-14
等於1995
)。
接下來,您忘記鏈接boost_date_time庫:
從全新的16.04機器開始:
apt update; apt install -yy build-essential libboost-all-dev
echo -e '#include <boost/date_time.hpp>\nint main(){std::cout<<boost::gregorian::to_simple_string({2018,9,14})<<std::endl;}' > test.cpp
g++ -std=c++11 test.cpp -lboost_date_time && ./a.out
作品和印刷品
2018-Sep-14
教一個人如何捕魚: 什么是未定義的參考/未解決的外部符號錯誤,我該如何解決?
顯示一個人吃魚:
你的dt初始化看起來很可疑。 如果你想要2018年9月14日,我想你想要{2018,9,14}。
更完整的代碼版本可能如下所示:
#include <boost/date_time.hpp>
int main(int argc, char* argv[]) {
const boost::gregorian::date dt{2018, 9, 14};
const std::string str = boost::gregorian::to_simple_string(dt);
std::cout << str << std::endl;
return 0;
}
這正確地產生了
2018-Sep-14
作為其輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.