Templates and UnResolved External Link Error! 
Author Message
 Templates and UnResolved External Link Error!

Hi,

What in the world don't I understand?

I am trying to zero in on a problem I am having instantiating a template class.

For testing purposes, I have generated a simple console app.   I have a header file named mytemplateclass1.h and this is what it contains :

    template<class T>
    class MyTemplateClass1
    {
    public:
         MyTemplateClass1( T& obj );
         ~MyTemplateClass1();
    private :
         T m_dataMember;
    };

I have a source file named mytemplateclass1.cpp and this is what it contains :

    #include "mytemplateclass1.h"

    template<class T>
    MyTemplateClass1<T>::MyTemplateClass1(T& obj)
        : m_dataMember(obj)
    {

    }

    template<class T>
    MyTemplateClass1<T>::~MyTemplateClass1()
    {

    }

Finally I have a file named main.cpp where I instantiate the above defined template class and this is what it contains :

    #include <mytemplateclass1.h>

    int main(int argc, char **argv)
    {
         int i = 100;
         MyTemplateClass1<int> templateClass1( i );

         return 0;
    }

Of course, it compiles file but I get these two linker errors  :
    Unresolved external 'MyTemplateClass1<int>::~MyTemplateClass1()' referenced from D:\TEMPTEST\MAIN.OBJ.
    Unresolved external 'MyTemplateClass1<int>::MyTemplateClass1(int&)' referenced from D:\TEMPTEST\MAIN.OBJ.

I know there is something I don't understand about the new C++ standard, since I get the same exact error in Visual C++ 6.0 and C++ Builder 6.0.  Any help would be greatly appreciated.

Louis C.
Abit Technology, NYC



Fri, 11 May 2001 03:00:00 GMT  
 Templates and UnResolved External Link Error!

There was a thread here not long ago where I posted a question that had
a lot to do with yours. Bottom line is that you have to include the cpp
file as well as the header one or implement the template class methods
inline. The template definition has to be available in the scope of its
use. Stroustroup's "export" keyword is not yet supported by compilers
but when it is I suppose you'll be able to export the template
definitions and avoid including the cpp file.
So inserting #include "mytemplateclass1.cpp" in main.cpp will solve your
problem.

Best Regards,
Alen

Quote:

>  Hi, What in the world don't I understand? I am trying to zero in on a
> problem I am having instantiating a template class. For testing
> purposes, I have generated a simple console app.   I have a header
> file named mytemplateclass1.h and this is what it contains :
> template<class T>
>     class MyTemplateClass1
>     {
>     public:
>          MyTemplateClass1( T& obj );
>          ~MyTemplateClass1();
>     private :
>          T m_dataMember;
>     }; I have a source file named mytemplateclass1.cpp and this is
> what it contains :     #include "mytemplateclass1.h"
> template<class T>
>     MyTemplateClass1<T>::MyTemplateClass1(T& obj)
>         : m_dataMember(obj)
>     {

>     }     template<class T>
>     MyTemplateClass1<T>::~MyTemplateClass1()
>     {

>     } Finally I have a file named main.cpp where I instantiate the
> above defined template class and this is what it contains :
> #include <mytemplateclass1.h>    int main(int argc, char **argv)
>     {
>          int i = 100;
>          MyTemplateClass1<int> templateClass1( i );          return 0;

>     } Of course, it compiles file but I get these two linker errors
> :    Unresolved external 'MyTemplateClass1<int>::~MyTemplateClass1()'
> referenced from D:\TEMPTEST\MAIN.OBJ.    Unresolved external
> 'MyTemplateClass1<int>::MyTemplateClass1(int&)' referenced from
> D:\TEMPTEST\MAIN.OBJ. I know there is something I don't understand
> about the new C++ standard, since I get the same exact error in Visual
> C++ 6.0 and C++ Builder 6.0.  Any help would be greatly
> appreciated. Louis C.Abit Technology, NYC



Fri, 11 May 2001 03:00:00 GMT  
 Templates and UnResolved External Link Error!

Not necessarily. There is an interesting but highly useful procedure in the standard called "explicit instantiation" which some purists may not like but which definitely has some advantages. Assuming VC++ now supports this (I don't know if it does), at the top of your ".cpp" file, prior to the actual template definitions, add the following lines:

#ifdef _DEBUG
    #include "mytemplateclass1.h"
    template MyTemplateClass1<int>;
#endif

and at the bottom of "mytemplateclass1.h" add:

#ifndef _DEBUG
    #include "mytemplateclasses.cpp"
#endif

In the first block above, you should add one such line for each type you wish to instantiate ("int" in this case). This causes an instance of the class to be generated for each type you define but the linker should ultimately remove those that aren't actually used, if any (hopefully). There are pros and cons to this method I won't get into but most of the disadvantages are based in theory so don't worry. In any event, as seen, the debug version of your code will instantiate the classes ahead of time (explicitly) while the release version will do it in the usual way (implicitly). Note that besides allowing you to separate your implementation as usual, the above method also prevents long rebuilds that normally occur when you change template based code directly in the header itself (that is, when templates are #included there as is usually the case). That's because changes to the header (obviously) cause recompiliation of every file that #includes that header. That can really slow your development. Another advantage is that you can easily see in one location at the top of your ".cpp" file evey instance of that template in your entire application. Beats having to scan your source for every implicit use of it. Anyway, you'll have to test this since as I said, I don't know if VC++ supports this yet.

    There was a thread here not long ago where I posted a question that had a lot to do with yours. Bottom line is that you have to include the cpp file as well as the header one or implement the template class methods inline. The template definition has to be available in the scope of its use. Stroustroup's "export" keyword is not yet supported by compilers but when it is I suppose you'll be able to export the template definitions and avoid including the cpp file.
    So inserting #include "mytemplateclass1.cpp" in main.cpp will solve your problem.
    Best Regards,
    Alen


         Hi, What in the world don't I understand? I am trying to zero in on a problem I am having instantiating a template class. For testing purposes, I have generated a simple console app.   I have a header file named mytemplateclass1.h and this is what it contains :     template<class T>
            class MyTemplateClass1
            {
            public:
                 MyTemplateClass1( T& obj );
                 ~MyTemplateClass1();
            private :
                 T m_dataMember;
            }; I have a source file named mytemplateclass1.cpp and this is what it contains :     #include "mytemplateclass1.h"     template<class T>
            MyTemplateClass1<T>::MyTemplateClass1(T& obj)
                : m_dataMember(obj)
            {

            }     template<class T>
            MyTemplateClass1<T>::~MyTemplateClass1()
            {

            } Finally I have a file named main.cpp where I instantiate the above defined template class and this is what it contains :     #include <mytemplateclass1.h>    int main(int argc, char **argv)
            {
                 int i = 100;
                 MyTemplateClass1<int> templateClass1( i );          return 0;
            } Of course, it compiles file but I get these two linker errors  :    Unresolved external 'MyTemplateClass1<int>::~MyTemplateClass1()' referenced from D:\TEMPTEST\MAIN.OBJ.    Unresolved external 'MyTemplateClass1<int>::MyTemplateClass1(int&)' referenced from D:\TEMPTEST\MAIN.OBJ. I know there is something I don't understand about the new C++ standard, since I get the same exact error in Visual C++ 6.0 and C++ Builder 6.0.  Any help would be greatly appreciated. Louis C.Abit Technology, NYC



Sat, 12 May 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Templates in library giving unresolved external when linked to app

2. templates & LNK2001: unresolved external symbol error

3. VC++ 7 Link Error : unresolved external symbol

4. Linking error 2019 unresolved external symbol _main when porting to .NET

5. Linking error 2019 unresolved external symbol _main ...

6. msvc link-error: _printf unresolved external

7. Linking Problem when trying to create ReleaseUMinDependency[error LNK2001: unresolved external symbol _main]

8. Linking Errors - unresolved external symbol _SQLConnect@28

9. link error LNK2001 - unresolved external symbol

10. Link error.(unresolved external symbol _main)

11. NtCreateFile() unresolved external link error in VC++ 4.2

12. linking error Unresolved external symbol in eVC4

 

 
Powered by phpBB® Forum Software