Dear Advanced c/g++ programers:
I copied and tested a piece of simple formatting a datetime string code from
book, c++ cookbook, page 201, Chapter5 section2, Example5-4.
but it wont compile in my g++ 4.5.2(on ubuntu 10.04, kernel2.6.35-25)
----------------------------------------------------------------------
// Example 5-4 formatting a datetime string
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <stdexcept>
#include <iterator>
#include <sstream>
using namespace std;
ostream& formatDatetime(ostream& out, const tm& t, const char* fmt) {
const time_put<char>& dateWriter = use_facet<time_put<char> >(out.getloc());
int n = strlen(fmt);
if (dateWriter.put(out, out, ' ', &t, fmt, fmt + n).failed()) {
throw runtime_error("failure to format date time");
}
return out;
}
string dateTimeToString(const tm& t, const char* format) {
stringstream s;
formatDateTime(s, t, format);
return s.str();
}
tm now() {
time_t now = time(0);
return *localtime(&now);
}
int main()
{
try {
string s=dateTimeToString(now(), "%A %B, %d %Y %I:%M%p");
cout << s << endl;
s=dateTimeToString(now(), "%Y-%m-%d %H:%M:%S");
cout << s << endl;
}
catch(...) {
cerr << "failed to format date time" << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
----------------------------------------------------------------------------
eric@eric-laptop:~/cppcookbook/download$ g++ 5-4.cpp
5-4.cpp: In function ‘std:
stream& formatDateTime(std:
stream&, const tm&, const char*)’:
5-4.cpp:15:17: error: invalid use of incomplete type ‘const struct std::time_put<char>’
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/localefwd.h:163:11: error: declaration of ‘const struct std::time_put<char>’
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.h:815:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/iostream:40,
from 5-4.cpp:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.tcc: In function ‘const _Facet& std::use_facet(const std::locale&) [with _Facet = std::time_put<char>]’:
5-4.cpp:13:78: instantiated from here
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.tcc:107:43: error: incomplete type ‘std::time_put<char>’ used in nested name specifier
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/locale_classes.tcc:112:56: error: cannot dynamic_cast ‘* *(__facets + ((unsigned int)(((unsigned int)__i) * 4u)))’ (of type ‘const class std::locale::facet’) to type ‘const struct std::time_put<char>&’ (target is not pointer or reference to complete type)
eric@eric-laptop:~/cppcookbook/download$
-----------------------------------------------------------------------
source code
http://examples.oreilly.com/9780596007614/
you can download and check and run/test by yourself.
Need your help to fix it and thanks a lot in advance, Eric