2011-11-04 7 views
5

Rozważmy następujący kod:Dlaczego program iostream to time.h?

#include <iostream> 

template<class C> 
struct time { }; 

int main() { } 

Produkuje (GCC 4.5):

error: ‘template<class C> struct time’ redeclared as different kind of symbol 
/usr/include/time.h:186:15: error: previous declaration of ‘time_t time(time_t*)’ 
  1. Dlaczego iostream zawierać time_t time(time_t*)?
  2. Dlaczego iostream zawiera time_t time(time_t*) poza obszarem nazw std?
  3. (bez odpowiedzi) Dlaczego, jeśli usunę ten kod template<class C>, nie otrzymam tego błędu?

Odpowiedz

6

Jeśli prowadzisz g++ -H -Wall -c testim.cc (gdzie testim.cc to przykład) zobaczysz, że

.... /usr/include/c++/4.6/bits/ios_base.h 
..... /usr/include/c++/4.6/ext/atomicity.h 
...... /usr/include/c++/4.6/x86_64-linux-gnu/./bits/gthr.h 
....... /usr/include/c++/4.6/x86_64-linux-gnu/./bits/gthr-default.h 
........ /usr/include/pthread.h 
......... /usr/include/sched.h 
.......... /usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h 
.......... /usr/include/time.h 

Więc <time.h> jest dołączone do obsługi pthread.

To z GCC 4.6 Debian/Sid/AMD64

+0

Wypróbuj na 4.6.2. Dziennik zmian mówi, że zależność od '' została zmniejszona. –

+0

Na moim komputerze g ++ to 'gcc version 4.6.2 (Debian 4.6.2-3)' –

2

Wydaje się, że dla wsparcia locale na moim polu:

$ cat whytime.cc 
#define _TIME_H_ 
#include <iostream> 

int main() { 
    std::cout << "hello, world" << std::endl; 
    return 0; 
} 

$ g++ -Wall -Werror -Wextra -ansi -o whytime whytime.cc && ./whytime 
In file included from /usr/include/c++/4.2.1/cwchar:52, 
       from /usr/include/c++/4.2.1/bits/postypes.h:46, 
       from /usr/include/c++/4.2.1/iosfwd:49, 
       from /usr/include/c++/4.2.1/ios:43, 
       from /usr/include/c++/4.2.1/ostream:45, 
       from /usr/include/c++/4.2.1/iostream:45, 
       from whytime.cc:2: 
/usr/include/c++/4.2.1/ctime:66: error: ‘::clock_t’ has not been declared 
/usr/include/c++/4.2.1/ctime:68: error: ‘::tm’ has not been declared 
/usr/include/c++/4.2.1/ctime:70: error: ‘::clock’ has not been declared 
/usr/include/c++/4.2.1/ctime:71: error: ‘::difftime’ has not been declared 
/usr/include/c++/4.2.1/ctime:72: error: ‘::mktime’ has not been declared 
/usr/include/c++/4.2.1/ctime:73: error: ‘::time’ has not been declared 
/usr/include/c++/4.2.1/ctime:74: error: ‘::asctime’ has not been declared 
/usr/include/c++/4.2.1/ctime:75: error: ‘::ctime’ has not been declared 
/usr/include/c++/4.2.1/ctime:76: error: ‘::gmtime’ has not been declared 
/usr/include/c++/4.2.1/ctime:77: error: ‘::localtime’ has not been declared 
/usr/include/c++/4.2.1/ctime:78: error: ‘::strftime’ has not been declared 
In file included from /usr/include/c++/4.2.1/locale:46, 
       from /usr/include/c++/4.2.1/bits/ostream.tcc:46, 
       from /usr/include/c++/4.2.1/ostream:572, 
       from /usr/include/c++/4.2.1/iostream:45, 
       from whytime.cc:2: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘_InIter std::time_get<_CharT, _InIter>::_M_extract_via_format(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*, const _CharT*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1839: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1846: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1854: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1861: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1873: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1880: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1883: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1895: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1900: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1908: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1912: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1932: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1968: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1976: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_weekday(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:2210: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_monthname(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:2259: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_year(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:2288: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
+0

Dobra sztuczka, ale nie dostaję twojego błędu –

+0

Definiując strażnik nagłówka 'time.h', wymuszamy jego włączenie, aby nic nie robić. Przypuszczalnie to, co zawiera, również użyje go wkrótce, a wynikłe błędy kompilacji złapią włącznik na gorącym uczynku. Dla mnie wydaje się to być '/ usr/include/C++/4.2.1/locale' wprowadzone przez' ostream'. Należy pamiętać, że pokazuje to tylko jeden włącznik, mogą być inne. – phs

1

Widocznie iostream potrzebuje czegoś GCC w time czegoś.

time_t jest rzeczą C język, który nie ma nazw, a więc dla wstecznej porównywalności-owski, to jest zarówno w przestrzeni nazw std, a nie w przestrzeni nazw std.

Co do tego, dlaczego nie ma błędów, nie jestem w 100% pewny. Wiem, że C może mieć funkcje i struktury o tej samej nazwie, a struct s zostały zakwalifikowane pod nazwą struct, aby kompilator mógł je rozdzielić. Kompilatory C++ uważają, że jest to funkcja, chyba że poprzedza ją słowo kluczowe struct. http://ideone.com/XZB2M v http://ideone.com/kWMKE. Sądzę, że pozwala to również na porównywalność wsteczną?

+0

phs mówi, że zawiera 'ctime' dla obsługi locale, co ma sens. –

+0

ok, ale co z "szablonem"? –

+0

Nie mam absolutnie pojęcia o tym. Przepraszam :( –

Powiązane problemy