mike.h on Tue, 18 Feb 2003 00:44:11 -0500 |
Jeff, Correct on both counts. A template is like a macro. If you use a separate .c or .cpp file for implementation, it won't get the substitutions of the "real" types at compile time, and so you'll get an "incomplete type" or "no matching function" error. Put implementation code inside the template declaration in the header, include it, and it will compile and run without a separate .c or .cpp implementation file. I know that this may break the "don't put anything that allocates storage in a header" rule, but AFAIK it's the only way to derive from a class template. If anyone knows different, please enlighten me. On Sun, 2003-02-16 at 20:08, Jeff Abrahamson wrote: > On Sun, Feb 16, 2003 at 07:17:42PM -0500, Jeff Abrahamson wrote: > > Looking for more syntax help, I've gotta get a copy of version 3 of > > Stroustrup soon. > > > > I'm trying to derive from a templated class, but I'm not getting the > > syntax right. The C++ faq and google aren't helping me here. Pointers > > or outright answers appreciated. > > > > Thanks much. Here's the simple example: > > > > > > template <class T> > > class SwapArray { > > > > }; > > > > > > class Bar : public template <class T> SwapArray { > > //class Bar : public SwapArray { > > > > }; > > The answer, I think, is two fold: > > First, since Bar makes specific the class T, it has to specify it: > > class Bar : public SwapArray<Foo> { ... }; > > Second, forward declarations of template classes aren't good enough: > the entire class definition must precede it, even though this means an > include of a .h in another .h. > > Indeed, the implementation of a template class has to go with the > template classes declaration, near as I can tell, since the compiler > needs to be able to generate the appropriate code at compilation time > for the code that uses the template class. > > -- > Jeff > > Jeff Abrahamson <http://www.purple.com/jeff/> > GPG fingerprint: 1A1A BA95 D082 A558 A276 63C6 16BF 8C4C 0D1D AE4B -- -mike.h _________________ mike.h@acm.org mike.h@stemik.com __________________________________________ Democracy is the worst form of government; except for all those other forms that have been tried from time to time. -Winston Churchill Attachment:
signature.asc
|
|