1

Possible Duplicate:
Why can templates only be implemented in the header file?

hello everyone I've just read that if I write some template I must write all code in header file (including constructor, desctructors...), but I can't understand why, can somebody please explain?

Community
  • 1
  • 1
rookie
  • 7,723
  • 15
  • 49
  • 59

2 Answers2

0

Because the templated code can be compiled only after it is know, which class/type replaces the template, so all code must be available then.

There is a very detailed explanation of the details in the C++ FAQ

fschmitt
  • 3,478
  • 2
  • 22
  • 24
0

When a template is used, it's much like the defined template class or function is used to create a new class by replacing the template parameters with the provided parameters.

The code for this new class is compiled right when it's needed, so all the source needs to be there at that time.

See this link at the bottom of the page for a bit more detail.

JoshD
  • 12,490
  • 3
  • 42
  • 53