Possible Duplicate:
Why can templates only be implemented in the header file?
Why won't this code link when compiled in separate files demonstrated here. Here is the compilation line
g++ ./templateC.cpp ./main.cpp
here is the error
main.cpp:(.text+0x11): undefined reference to `templateC<int>::templateC()'
collect2: ld returned 1 exit status
I always thought you could place the implementation in a separate file than the class. If I can't, why is that?
All files are watered down to prevent confusion, but were compiled with the error as shown here.
Here are my files
main.cpp
#include "templateC.h"
main(int argc, char *argv[])
{
templateC<int> t;
}
templateC.h
template<class T>
class templateC
{
public:
templateC();
};
and templateC.cpp
#include "templateC.h"
template<class T>
templateC<T>::templateC()
{
}