in g++(invalid use of incomplete type)

E

eric

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 ‘std::eek:stream&
formatDatetime(std::eek:stream&, const tm&, const char*)’:
Example5-4.cpp:16: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>’
Example5-4.cpp: In function ‘std::string dateTimeToString(const tm&,
const char*)’:
Example5-4.cpp:24:31: error: ‘formatDateTime’ was not declared in this
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 ‘const _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
‘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/ch5$
 
E

eric

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 ‘std::eek:stream&
formatDatetime(std::eek:stream&, const tm&, const char*)’:
Example5-4.cpp:16: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>’
Example5-4.cpp: In function ‘std::string dateTimeToString(const tm&,
const char*)’:
Example5-4.cpp:24:31: error: ‘formatDateTime’ was not declared in this
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 ‘const _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
‘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/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 said:
(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;
}
 
I

Ian Collins

-----------------------------------------------------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) {

In which standard header is time_put declared? Check for balance of <
and >.
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);

Names are case sensitive!
 
M

Michael DOUBEZ

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 ‘std::eek:stream&
formatDatetime(std::eek:stream&, const tm&, const char*)’:
Example5-4.cpp:16: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>’
Example5-4.cpp: In function ‘std::string dateTimeToString(const tm&,
const char*)’:
Example5-4.cpp:24:31: error: ‘formatDateTime’ was not declared in this
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 ‘const _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
‘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/ch5$
[...]
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <stdexcept>
#include <iterator>
#include <sstream>

#include <locale>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top