Wednesday, September 12, 2007

Example of template linking error

B.H
template '<' class t '>'
class b
{
public:
b() ;
~b() ;

} ;

// B.CPP
#include "B.H"
template '<' class t '>'
b'<' t '>'::b()
{
}
template '<' class t '>'
b'<' t>::~b()
{
}


//MAIN.CPP
#include "B.H"
void main()
{
b '<' int '>' bi ;
b '<'float '>' bf ;
}

When compiling b.cpp, the compiler has both the declarations and the definitions available. At this point the compiler does not need to generate any definitions for template classes, since there are no instantiations. When the compiler compiles main.cpp, there are two instantiations: template class
b '<'int'>' and b'<'float'>'. At this point the compiler has the declarations but no definitions!

If do g++ main.cpp ,error will come
if we write g++ main.cpp b.cpp the result is same.
Solution to this we have allready discussed, pls see in my site itselt template instatiation problem.
Regards,
sahu

No comments: