TITLE: forward declaring i/o constructs (Newsgroups: comp.lang.c++.moderated, 2 Feb 98) CUTHBERT: dacut@henry.ece.cmu.edu (David A. Cuthbert) > (btw, I'm not completely serious about this. I do think that writing > "class ostream;" is comparable to legacy C code (half-ANSI, half-K&R) > that has "void printf();" in the code rather than > "#include ".) AUSTERN: Matt Austern Actually, writing "class ostream" is wrong for two reasons---and I really do mean wrong; I'm not just giving my opinion about style. First, it's not ostream but std::ostream. Second, std::ostream isn't a class at all, but a typedef. So it's not enough to just forward-declare ostream; you have to declare the class that it actually stands for. A full forward-declaration for ostream is actually rather tricky; it would look something like this. namespace std { template class char_traits; template<> class char_traits; template class basic_ostream; typedef basic_ostream > ostream; } Note that the standard library contains a header, , that does exactly this. --------------------------------------------------------------------- (Newsgroups: comp.lang.c++.moderated, 30 Aug 99) KALB: Jon Kalb >I am trying to get the syntax right to forward declare a std::ostream. NARAN: Siemel B. Naran You can't forward declare the standard library headers. One reason is that the compiler is allowed to do special optimizations trigerred by "#include " or "#include ". Another reason is that the compiler may store pre-compiled versions of the library files, possibly in the compiler itself. The a statement like "#include " unlocks the pre-compiled version. A third reason is that the class or function may have additional default arguments. Maybe a library implementor has used template , size_t Block=16> class vector; The third template argument is non-standard, but it is reasonable. Then if you tried this // forward declaration namespace std { template class vector; }; your code would compile on most compilers and libraries. But the code would not compile on the compiler and library that uses the third "size_t Block" template argument. Take a look at to declare the standard iostreams. There you'll also learn the proper way to forward declare iostream. The issue is that std::ostream is a typedef, not a class. There is no way to forward declare std::deque, etc. Another option is to declare an implementation in the .hh file and define it in the .cc file. // myfile.hh namespace myspace { class ostream; void Print(ostream); }; // myfile.cc #include "myfile.hh" #include struct myspace::ostream { std::ostream& o; ostream(std::ostream& o) : o(o) { } }; void myspace::Print(myspace::ostream strm) { ... };