Sign in to follow this  
Woltan

DLL import with a template class

Recommended Posts

Hey folks, I once again rely on your help. This time regarding the import of a template based class in a dll and I hope this is the right place to post. This is my source: HEADER: #ifndef TEST_H #define TEST_H template <typename TYPE> class Test { public: __declspec(dllexport) Test(TYPE *p); private: TYPE *dummy; }; #endif CPP FILE: #include "Header//Test.h" template <typename TYPE> __declspec(dllexport) Test<TYPE>::Test(TYPE *p) { dummy = p; return; } Everything works out just fine. A dll and a lib are created but in the project where I need the dll I have trouble doin this: #pragma comment(lib,"TestDLL.lib") #include <stdlib.h> #include "Test.h" //(I changed dllexport to dllimport int main() { int *i = new int(5); <--- no probs here Test<int> *p = NULL; <--- no probs here p = new Test<int>(i); <--- Problem! return 0; } In Visual C++ the error message is this: Unresolved external symbol "__declspec(dllimport) public: __thiscall Test<int>::Test<int>(int *)" (__imp_??0?$Test@H@@QAE@PAH@Z) At least I hope it is cause I do not have an english version of visual. Well I hope I pointed out my problem sufficiant and at the correct location, and of cause I hope you can help me with this one. Cherio Woltan

Share this post


Link to post
Share on other sites
You can't have a template of anykind in a dll. In templates you must include the deffinition in the files that use them. Other wise you will have to create a seperate class for seperate datatypes. The reason for this is because templates are only compiled for the datatypes they are used with.

Share this post


Link to post
Share on other sites
Okey, thanks for the quick help. Too bad that it is not possible to do that. Do you have any suggestions how I can work around that problem. OK the article says you can export an instance in a dll, but that woldn't solve my problem.
Thanks for any help
regards Woltan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this