Archived

This topic is now archived and is closed to further replies.

CommanderXXL

template class member functions defining outside the class statement

Recommended Posts

CommanderXXL    122
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

Share this post


Link to post
Share on other sites
Shannon Barber    1681
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.

Share this post


Link to post
Share on other sites
Dactylos    122
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
This worked for me in VS7.

  
//.h

template<class T> class LL {
public:
LL();
};

//.cpp

template <class T> LL<T>::LL() {
...
}

Share this post


Link to post
Share on other sites
Shannon Barber    1681
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);
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.h

template<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>();
}

Share this post


Link to post
Share on other sites