1

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()
{

}
Community
  • 1
  • 1
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
  • 2
    Have a look at this: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13 – razlebe Feb 03 '12 at 15:42
  • I read through it, why must that be done, is it a bug in the linker? – rubixibuc Feb 03 '12 at 15:46
  • @MadKeithV You're right I didn't see that post, should I delete the question or link to it? – rubixibuc Feb 03 '12 at 15:48
  • @rubixibuc: no need to delete your question. Having duplicates is not such a bad thing, it makes the "original" question easier to find through search engines. See [here](http://meta.stackexchange.com/questions/32311/do-not-delete-duplicates) for a reference for this, there are many more on [meta]. – Mat Feb 03 '12 at 17:14

0 Answers0