0

I installed boost from synaptic.

Now i need to convert date from/to string, but when I write code like below,

date dt{2018-9-14};

string str=to_simple_string(dt);

cout<<str<<endl;

it gives an error:

/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'

How can I solve this???

Dean Seo
  • 5,486
  • 3
  • 30
  • 49

2 Answers2

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:

enter image description here

sehe
  • 374,641
  • 47
  • 450
  • 633
  • "eating the fish" ftw, go @sehe :-) – moodboom Mar 26 '18 at 00:38
  • #include #include #include "boost/date_time.hpp" #include "boost/date_time/gregorian/gregorian.hpp" using namespace std; using namespace boost::gregorian; int main() { date dt{2018,10,15}; string str_date=to_simple_string(dt); cout< – Anamul Hasan Mar 27 '18 at 09:50
  • You missed the point. I have you the linker option for a reason. As well as a link to the [tag:c++-faq] answer – sehe Mar 27 '18 at 09:53
0

Your initialization of dt looks suspicious. I think you want dt{2018, 9, 14} if you want 14th Septmber 2018.

A fuller version of your code might look like:

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

which correctly produces

2018-Sep-14

as its output.

rwp
  • 1,786
  • 2
  • 17
  • 28
  • But,same error in my computer while executing your code. i am using boost 1.58 . ubuntu 16.04 machine. is there anything else to solve this problem???? – Anamul Hasan Mar 25 '18 at 08:17
  • Curious - I'm using a very similar system, with boost-1.62, and don't think these aspects of boost::date_time have changed much since version1.40 or earlier. My compile line is "g++ test.cpp -lboost_date_time -I/usr/include/boost". Perhaps you could give a bit more detail of your own code & compilation steps? – rwp Mar 25 '18 at 08:38
  • One thing worth checking with Ubuntu-16.04 is whether you've enabled C++11 support within the compiler - using the braced initializer syntax is a C++11 feature. You could try adding '-std=c++11' to your compiler options. – rwp Mar 25 '18 at 08:46
  • i am using netbeans for c++. i installed lib boost 1.58 from synaptic. all date time functionality i can use rather than conversion. is there any need to configure netbeans for boost. thank you very much brother. – Anamul Hasan Mar 25 '18 at 08:47