Templates and Unresolved Externals
-
I'm trying to implement a class not unlike CList, XList. I'm using a template template <class T> class XList and when I compile I get unresolved externals errors for my constructor and deconstructor. I originally got this error for all my functions but I fixed it by adding 'virtual' to the front of the declaration. This doesn't seem to work with the aforemention functions. I'm declaring them thus: XList(); virtual ~XList(); template <class T> XList<T>::XList() { // Constructor Code } template <class Tglt; XList<T>::~XList() { // Deconstructor code } Any help would be appreciated - X
-
I'm trying to implement a class not unlike CList, XList. I'm using a template template <class T> class XList and when I compile I get unresolved externals errors for my constructor and deconstructor. I originally got this error for all my functions but I fixed it by adding 'virtual' to the front of the declaration. This doesn't seem to work with the aforemention functions. I'm declaring them thus: XList(); virtual ~XList(); template <class T> XList<T>::XList() { // Constructor Code } template <class Tglt; XList<T>::~XList() { // Deconstructor code } Any help would be appreciated - X
Did you put the function definitions into the same file where your template class is declared (e.g. into the header file and not the .cpp file)? The compiler requires the functions definitions to be in the same translation unit than the class declaration. Oliver
-
Did you put the function definitions into the same file where your template class is declared (e.g. into the header file and not the .cpp file)? The compiler requires the functions definitions to be in the same translation unit than the class declaration. Oliver
Thanks, it worked - X
-
Did you put the function definitions into the same file where your template class is declared (e.g. into the header file and not the .cpp file)? The compiler requires the functions definitions to be in the same translation unit than the class declaration. Oliver
Did you mean that it's not possible to declare the class in a h file and the implementation in a cpp file? I have the same trouble as I convert my list which use void* to use template. But I don't move the code from cpp to h and when I link the library, it's ok. But when I link the application , I get link error. Does it exist another way to move the implementation from cpp to h for solving this kind of problem? Bruno
-
Did you mean that it's not possible to declare the class in a h file and the implementation in a cpp file? I have the same trouble as I convert my list which use void* to use template. But I don't move the code from cpp to h and when I link the library, it's ok. But when I link the application , I get link error. Does it exist another way to move the implementation from cpp to h for solving this kind of problem? Bruno
When you use your template class in more than one source file you should put the class declaration and the function definition in the same header file. Most template libraries like STL, ATL, WTL ship without cpp files. Oliver