Archived

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

[bobafet]

a templated class among multiple source files

Recommended Posts

Ok.. before I get into my problem, let me show you a little code: let''s say we have three files in a win32 console application (using visual c++ 6.0) main.cpp -------- #include "test.h" int main() { CTest foo; foo.Set(5); return 0; } test.cpp -------- #include "test.h" template void CTest::Set(itemType newValue) { Value = newValue; } test.h ------ template class CTest { public: void Set(itemType newValue); private: itemType Value; }; Now.. when I compile this code, I get this linker error: error LNK2001: unresolved external symbol "public: void __thiscall CTest::Set(int)" Please help me! =] I''ve been stuggling over this problem for about 5 hours now.. somebody must know thanks in advance =]

Share this post


Link to post
Share on other sites
I''m pretty sure with template classes you have to include the definition for the functions in the header file, unless you do some quirky compiler thingo, and i dont have any references here so i coudn''t tell you what it is.

Share this post


Link to post
Share on other sites
Well.. what I''m really trying to do is have a templated class inside a .lib file. When I try to use the .lib in a project it gives me that linker error (it has to be possible to put templated classes inside .lib files). Can anyone help me on this subject?

Share this post


Link to post
Share on other sites
No offence, but template in a lib file doesn't really make sense. Compiler will generate code when you specialize and use your template. So, if in your main.cpp you say

    
CTest<int> foo; // not CTest foo; that wouldn't compile



your compiler will substitute int for itemType, generate machine code and place it in main.obj.

The reason you cannot put a template a in a library is because different machine code is generated from the same source by the compiler depending on how you specialize the template.

Hope this helps.


Edited by - vladg on November 4, 2000 6:19:10 AM

Share this post


Link to post
Share on other sites
Actually, you should _eventually_ be able to do that, once compilers support it. The compilers should then be able to make the actual classes in the project from the template versions stored in the library. I wouldn''t hold my breath waiting though.

It''s better to put the templates in some header files and just include those in whatever project you want to use them.



- null_pointer
Sabre Multimedia

Share this post


Link to post
Share on other sites
You already can do it, the only problem is you need to explicitly tell the compiler to create the template class with the types you want. Once you''ve done that it works just like any other class (but it doesn''t work with member templates). To do that you need to use explicit template instantiation.


// Test.h

template<class T>
class Test
{
public:
void foo() const;

};

// Test.cpp

template<class T>
void Test<T>::foo() const
{
cout << typeid(T).name() << endl;
}

template class Test; // Tell the compiler to create a version
// of this template for ''int''


Doing it this way can also cut down on build times, and changing the template implementation wont force a rebuild of all the classes that use it. The problem is you need to edit the .cpp file each time you want to use it with a different type. Or you could just wait for a compiler to support ''export''

It''s not perfect, but it is useful if you want the templates to be in a .lib or .dll.

Share this post


Link to post
Share on other sites