Re: in g++(invalid use of incomplete type)
On Jul 10, 11:32 pm, eric <cneric12l...@gmail.com> wrote:
Dear c++ programers:
If you are also an g++ programers, especially ever create/modify its
source code, I need your help
I have a piece simple code about formatting a date/time as a string
from a book. its authors claim it work in vc++7.1 on xp.
but my g++4.5.2 didn't pass it on compile stage
-------------------------------------------------------------------------=
------------------
eric@eric-laptop:~/cppcookbook/ch5$ g++ Example5-4.cpp
Example5-4.cpp: In function =91std::ostream&
formatDatetime(std::ostream&, const tm&, const char*)':
Example5-4.cpp:16:17: error: invalid use of incomplete type =91const
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 =91const struct
std::time_put<char>'
Example5-4.cpp: In function =91std::string dateTimeToString(const tm&,
const char*)':
Example5-4.cpp:24:31: error: =91formatDateTime' was not declared in thi=
s
scope
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 Example5-4.cpp:2:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/
4.5.2/bits/locale_classes.tcc: In function =91const _Facet&
std::use_facet(const std::locale&) [with _Facet =
std::time_put<char>]':
Example5-4.cpp:14:77: 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
=91std::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 =91*
*(__facets + ((unsigned int)(((unsigned int)__i) * 4u)))' (of type
=91const class std::locale::facet') to type =91const struct
std::time_put<char>&' (target is not pointer or reference to complete
type)
eric@eric-laptop:~/cppcookbook/ch5$
-------------------------------------------------------------------------=
---------------------------------------------------------------------------=
--
I welcome anyone can leading me to modify the source code as well as
any short method to get around it.
thanks a lot in advance
Eric
-----------------------------------------------------the program I
used to compile is-----------------------
// 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;
}
--------------------------------------------------------------
or you can get from its download site
http://examples.oreilly.com/9780596007614/
5-4.cpp