Re: what about the template error??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"ymsboy" <ymsboy@xxxxxxx> wrote in message
news:%23MAeH0f3FHA.2676@xxxxxxxxxxxxxxxxxxxxxxx
> i created a template class in VC 7 like this:
> ////////////// testTemp.h ///////////
> #pragma once
> using namespace std;
> template<class T,int MAXMIZE>
> class Param{
> public:
> void Print();
>
> };
> ////////////// testTemp.cpp ///////////
> #include "stdafx.h"
> #include "testTemp.h"
> template<class T,int MAXMIZE>
> void Param<T,MAXMIZE>::Print()
> {
> std::cout<< MAXMIZE;
> }
>
> when i used the class like this:
>
> Param<int,67> p;
> p.Print();
>
> i received a link error:
> TempLateTest.obj : error LNK2019: unresolved external symbol "public: void
> __thiscall Param<int,67>::Print(void)" (?Print@?$Param@H$0ED@@@QAEXXZ)
> referenced in function _main
>
> where is my error? please help me!
>
>

I take it that the third section of your posted code is in some other file
that includes testTemp.h.

The problem is that templates are compiled when they are used, as it's only
at that point that the compiler knows the template parameters. The compiler
comes sees testTemp.cpp and will decide that there is nothing to compile
there, and merrily ignore it.

When the compiler comes accross Param<int, 67> p, p.Print(), it's only then
that it knows all the details of the template class that are required. The
trouble is, at that point, the compiler can no longer see the implementation
in testTemp.cpp, so no method body get's compiled and you get the unresolved
symbol.

There are two solutions:

1 - Have the method implementation in the header file (look at the STL
headers, that's how they do it). This makes the implenetation visible to the
compiler at the correct time.

2 - Use explicit template instantiation. This is where you tell the compiler
to create bodies for all functions of the template for certain template
parameters. In your example, you would place the line:

template class Param<int, 67>;

In the .cpp file (I placed it before the implementation, just so you see it
at the top of the file), to tell the compiler to create bodies for all
members of Param<int, 67>, then it'll compile just fine,

Simon



.



Relevant Pages

  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... templates let the compiler do more work ... The worst thing is when a function throws an exception ... And of course, exception specifications should work like people think they work, not as they /do/ work. ...
    (comp.arch.embedded)
  • Re: Unresolved External Symbol - problem with template class
    ... with Comeau C++ compiler. ... The problem is that the template class' member ... In Visual Studio 6, 7 and 7.1, the template definitions must be in the same ...
    (microsoft.public.dotnet.languages.vc)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... In particular, templates let the compiler do more work at compile time, and save the target from work at run time. ... Templates can be great for generic libraries, and do not necessarily create code bloat. ... You can use RAII even without exceptions, but obviously you have to handle your errors explicitly. ...
    (comp.arch.embedded)
  • Re: [C++] Vector - guaranteed to be contiguous?
    ... >> member is templated on the input iterators as well. ... template<typename OUTITER, typename INITER, typename INITERT> ... What happens is that the compiler sees copy and thinks, "Hey, I know copy. ... But it is a template, let me try to deduce those types. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: generic programming is very powerful. c should support it
    ... Where the compiler matches the correct template. ... already happens differently on different implementations. ...
    (comp.lang.c)