Template class h/cpp

Started by
7 comments, last by Prototype 21 years, 4 months ago
I have a problem with my templated list class when I split it up in h/cpp. Classes that use it come up with unresolved externals. Now I thought I read somewhere template class methods must remain in the .h file. Is that correct? I''m using VC6.
Advertisement
you must have entire template definitions in the headers (or, generally, defined before you use them).
tnx
But does it also count for the member functions'' implementation? Just to get it straight.
Normally defs->h code->cpp? What happens here?
declarations ''declare'' that something exists.
definitions ''define'' how it''s done.

as niyaw says, the definitions go in the headers. at the same time they act as declarations.

quote:Original post by Prototype
tnx
But does it also count for the member functions'' implementation? Just to get it straight.
Normally defs->h code->cpp? What happens here?


Templates, just like inline functions go in headers, since their implementation must be visible from the instanciation point.

That''s all.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ah thanks. One obscurity less.
I''ve switched to VC recently after years of Delphi and including gives me a headache in C++. The rest of the transition went quite smooth though.
this is only for templates though.

common practice for non-template classes/functions is to put the definition (ie implementation) in a cpp file.
You can also keep the implementation in the cpp file and at the end of the header file you include the cpp file.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
that would just be confusing and needless

This topic is closed to new replies.

Advertisement