troubles with templates
-
Why does it work that way b) and not a)? a) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var); }; template void some_class::callfunc(T var) { } #endif b) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var){} }; #endif //main.cpp #include "SomeClass.h" void main(void) { some_class example; int nt=4; example.callfunc(nt); }
-
Why does it work that way b) and not a)? a) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var); }; template void some_class::callfunc(T var) { } #endif b) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var){} }; #endif //main.cpp #include "SomeClass.h" void main(void) { some_class example; int nt=4; example.callfunc(nt); }
Because templates are expanded as they are compiled - just including the .h with the function declared (not defined) is not enough This is the pain the arse problem that you usually have. Really, its much easier if you stick to the thin template pattern - short template code. Then, put it in at the same time as decliration - as in b). If you really want to split it up - you can put the defination at the bottom of the .h its declared in. Or, if you really want the defination in a .CPP file - you can (uck) #include the .CPP into the other CPP you are using the template in.