我从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'
我该怎样解决这个问题?
答案 0 :(得分:2)
As the other answer stated, the constructor is wrong (use commas, or you'll simply say 2018-9-14
which is equal to 1995
).
Next, you forgot to link the boost_date_time library:
Starting from a completely new and fresh 16.04 machine:
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
Works and prints
2018-Sep-14
Teach a man how to fish: What is an undefined reference/unresolved external symbol error and how do I fix it?
Show a man eating the fish:
答案 1 :(得分:1)
你的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
作为输出。