Possible Duplicate:
Why can templates only be implemented in the header file?
When I include MyClass.h and do:
MyClass<int, int> ccc = MyClass<int, int>();
ccc.myMethod1(3, 4);
I get a lot of errors telling undefined reference to constructor and methods … However when I include MyClass.cpp (which is not a proper why to code) there is no error ! How to fix that ?
I’m compiling under Code::Blocks using g++
The reason is that template classes are not compiled, only instantiated templates will be compiled.
Rule of thumb: don’t place template implementations in a
cppfile but either directly in the header or another file included by the header (if you want to part implementation from the interface).E.g:
myclass.h
myclass.inc