templates and header files

Hey guys,

i don't know if i am the one who is doing the wrong thing?

i have a class template defined in a header file
and its definition on a .cpp file

now when i try to derive from this class template or instantiates an object from
it, i get a link error.

however, when i inline the implementation of the class template, everything is OK!

I work on the visual studio 2008 environment, can someone help out?
as far as I know, in msvs2008 compiler is not possible a separate compilation for templates. use it all code in header.
do you know of any compiler that would enable separate .cpp files for template definition?

@marembo
You can't just separate templates like that. Read about export keyword. At least comeau and icc implemented templates export.
By the way, export is the feature requested to be removed or deprecated in c++0x : http://herbsutter.com/2009/10/23/deprecating-export-considered-for-iso-c0x/
the reasons described here http://www.drdobbs.com/184401563
Looking on newer articles: export is removed from c++0x (http://herbsutter.com/2010/03/13/trip-report-march-2010-iso-c-standards-meeting/)
quote:
Edison Design Group (EDG), the only company to ever implement export, is recommending export be removed or deprecated.
end quote
The cheap easy solution is to put the template implemtation in another header and just #include it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// mytemplate.h

#ifndef MYTEMPLATE_H_INCLUDED
#define MYTEMPLATE_H_INCLUDED

template <typename T>
class MyTemplate
{
  T AFunction();
};

#include "mytemplate.hpp"

#endif // MYTEMPLATE_H_INCLUDED 
1
2
3
4
5
6
7
8
9
10
11
12
// mytemplate.hpp

// looks just like a cpp file, but it isn't
#include "whatever.h"
#include "whateverelseyouneed.h"
#include "mytemplate.h"  // superfluous, but harmless

template <typename T>
T MyTemplate<T>::AFunction()
{
  // ...
}
Topic archived. No new replies allowed.