Template Class

I have defined two template classes A and B.

A has its two files - A.h and A.cpp
B has only one file - B.cpp

I am using both of them in main.cpp

So, if whenever I want to use a particular data type(say) of A and B.

I do:
 
template class A<int>;
template class B<int>; ------------> work

in main.

Why is it so?

The way I compile is
g++ main.cpp A.cpp

main has
1
2
#include "A.h"
#include "B.cpp" 


As far as I get it, the template definition has to be done after your class is defined. Is it correct?

To do it for class A. I have to do this in A.cpp. I don't want to change A.cpp file once it is created. What are the other efficient way to do it?

Thanks in advance.
What are you expecting the above lines to do? They do nothing.

You really need to move the contents of A.cpp and B.cpp to their header files, as otherwise you will need to #include the .cpp files directly whereever you use A or B, and that is very poor practice.
But even if I move the contents of A.cpp and B.cpp to their respective header files then again it is again the same because neways i have to include their header files. So, how does it solve the purpose on including the contents of .cpp files to their header files.

Still I am confused as to what should I do? Should the templates always be included completely in header files?? Or there is some trick to separate the header files and .cpp files
Template Specialization is the term. I want to know what is the best way to specialize a template so that I don't need to write a new specialization in the template .cpp file everytime I want to use a new spcialized class. Is there any way for that?
Maybe you forgot "<>" (if you want to specialize a template for type int) :
template<> class A<int>;


No my concern is not this. Everytime I need a spcialization I need to do that in template .cpp file. So, suppose in the future I want to use a user-defined object. Then I have to do its specialization in template .cpp file. I want a method so that I don't touch the template .cpp file.

And still I can do template specialization for my own choice of objects.
Typically template classes are implemented wholly in header files. You can put specializations in any file you like; you just need to make sure the compiler sees the general one first and the specialized one second.
Topic archived. No new replies allowed.