template class member functions defining outside the class statement

Started by
8 comments, last by CommanderXXL 22 years, 5 months ago
normaly i declare classes in header files like; class X { X(); }; then i would write i cpp file with: X::X() { .... } now i wrote my LINKEDLIST class which uses for storage a data element which is a template like template class LL { LL() { .... } }; this is the problem, when i write the class members in the header file directly like shown above all works fine, but when i try to write my cpp file with template LL::LL() { ... } i get an: unresolved external symbol "public: __thiscall LINKEDLIST::~LINKEDLIST(void)" error from the linker. So my question is: How can i write my member functions like i normaly do it? Is it impossible? Do i need to write all function stuff in my header file? By CommanderXXL
---------------------------- By CommanderXXLCommander@bloomm.de----------------------------
Advertisement
Have you declared and defined the deconstructor?

Your functions don''t have to be in the header; you can implement them wherever is appropriate and convenient for you.


I wanna work for Microsoft!
That''s not a template method, that''s a template class - and in both cases the definitions need to be in the header file.

You cannot define them in .cpp file w/MSVC.

Some compilers do some tricks and jump-the-hoops to let you define them in the .cpp - but they don''t get compiled the same way normal classes do.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
What compiler are you using?

Most compilers currently have little support for templates with separate declaration and implementation. According to the standard you should be able to use the ''export'' keyword to indicate that a template should be made available for instantiation at link-time (actually just before link-time, but almost the same).

Currently your best bet to get things working is to include the full implementation of the template-class in the header along with its declaration (until compilers start supporting the standard better).
Hmm, i see. I use MSVC.NET (7.0) and it seems that there were no improvements since 6.0 so i need to write it in the header file. Thank you all for your help.

By CommanderXXL
---------------------------- By CommanderXXLCommander@bloomm.de----------------------------
This worked for me in VS7.

  //.htemplate<class T> class LL {public:  LL();};//.cpptemplate <class T> LL<T>::LL() {  ...}  
quote:Original post by Anonymous Poster
This worked for me in VS7.

And it has worked for me several times in VS6.


I wanna work for Microsoft!
Doesn''t work for me with GCC 2.95.xx. Strange that this one crops up just when I need it ;-)
No didn''t work
Try using it from another .cpp file and actually calling some methods.

Everytime you use a template, it creates a new class - in order for this to work, when you instance a template class it needs access to the defintion of all the methods. There''s little point in placing them in .cpp file - they''d have to be re-compiled with the template parameters when you used them anyway.

I''ve heard rumors that some compilers let you build some special template "obj" files, that the compiler knows how to look-up when you use them - it''s alot of work on the compiler writers part for little benefit.


Check VS7 to see if you can pass intergal numbers as template parameters, and if you can do partial specialization (two things lacking in VS6)

  template<typename tSize>class Array{const int m_iSize;public:Array() : m_Size = tSize{}}class PartialTest{template<typename tPartial>tPartial Method(){int i = 12;return static_cast<tPartial>(i);}}  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This code compiled with VS7. The LL works, but I am not sure, if the rest of the code does what you want it to do.

  //main.cpp#include "Header1.h"void main() {  test();  return 0;}//Header1.htemplate<class T>class LL {public:  LL();};template<int tSize>class Array {  const int m_Size;public:  Array() : m_Size(tSize) {}};class PartialTest {public:  template<typename tPartial>  tPartial Method() {    int i = 12;    return static_cast<tPartial>(i);  }};void test();//Source1.cpp#include "Header1.h"template <class T>LL<T>::LL() {  //do stuff}void test() {  LL<int> ll;  Array<9> a;  PartialTest p;  p.Method<int>();}  

This topic is closed to new replies.

Advertisement