I have a 3 file static.h ,static .cpp and main.cpp ,
In the static.h ,class template Typec is declared having the static member variable and function and corresponding definitions is in the static.cpp
main.cpp is having the simple call to that class ..
these three files compiled successfully but throwing the link error like ..
1 2 3 4 5 6 7
Undefined first referenced
symbol in file
Typec<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getvalue()main.o
Typec<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getInstance()main.o
ld: fatal: Symbol referencing errors. No output written to Type
collect2: ld returned 1 exit status
make: *** [Type] Error 1 ...
on surfing, I found the reason in the internet ,it solve the problem but I didn't get exactly reason ..
reason is :-
It's not possible to separate template declarations and implementations into separate files. That is because templates don't produce code unless they are instantiated and the compiler has to see the complete template to produce code.
What you could do is #include "template.cpp" at the bottom of template.h.
can explain why we have to append the implementation file (.cpp) in the header...
The compilation model of C++ is borrowed from C and is relatively efficient if you make changes to some code that do not affect the places where it is used. The problem is that in order to use this compilation model effectively, you need to be able to compile the sources separately and link them at the end.
C++ templates are not something you can compile. Why? Because they are essentially parametrized source code generators and as such they can produce different "source code" depending on how you instantiate them. The compiler can not compile a source code generator. It needs the source code itself. But in order to get to the source code when the "generator" (i.e. the template) is used, it has to be able to "see" the source code generator during compilation. Compilation is performed on the basis of translation units. Those are roughly your source files with the header files expanded and preprocessed. The compiler uses only the information within the translation unit in order to compile it. It doesn't refer to anything external, because that would be the job of the linker. That's why you must include the code generator (template) in the translation unit (e.g. through a header file), so that the compiler can see it, use it to generate the necessary source code and then compile the source code to machine code (with hooks left for the linker to use). It is complex process overall. It is intuitive that whenever someone changes the code generator (templates) it will affect all sources that use it. It is as if the generator is separate program that you run before compilation and now you have to recompile the affected sources.
Other models of compilation are possible that circumvent the issue and decrease the compilation dependencies, but C++ does not support them very effectively with the current compiler architectures. The standard tried to introduce "export" keyword in order to allow separate compilation of the output from the code generator (template) and the sources that request the code generation (the sources that instantiate the template), but it was not widely adopted from compiler vendors and is now sort-of deprecated.
Regards
EDIT: Templates are similar to pre-processor macros in that way. You can not place a macro in a separate .cpp file and "compile it" and then link to it.