How do I persuade rootcint to accomodate a template class with a non-type parameter? A trivial example is included below illustrating the problem. I’m running ROOT version 4.04.
Error: link requested for unknown class TMyArray FILE:LinkDef.h LINE:7
Warning: Error occured during reading source files
Warning: Error occured during dictionary source generation
!!!Removing dict.C dict.h !!!
Error: /sw/bin/rootcint: error loading headers...
from rootcint. If I say
#pragma link C++ class TMyArray<2>;
Then rootcint executes without errors, but then when I try to build an executable MyROOT which has a TRint object and takes me to the standard CINT interface, I get this:
/usr/bin/ld: Undefined symbols:
TMyArray<2>::TMyArray()
collect2: ld returned 1 exit status
make: *** [MyROOT] Error 1
If I comment out the pragma line for TMyArray altogether, everything compiles, but obviously I then can’t get at TMyArray from inside CINT.
Machines I don’t own, but use, currently run root 4.04. No doubt I will eventually move to root 5.0x. 'Til then, if it’s a version issue, I’ll work around it.
/usr/bin/ld: Undefined symbols:
TMyArray<2>::TMyArray()
collect2: ld returned 1 exit status
make: *** [MyROOT] Error 1 This is a real error message about your code. You did not provide an implementation for the constructor.
The tar file attachment has approx 20 loc, including the above, plus Makefile & LinkDef.h. Apologies in advance if I’ve done something silly. It compiles for me as is, but as soon as I uncomment the pragma line for TMyArray in LinkDef.h, I get the error described previously.
Yes you did provide it … but it is never instantiated … The easiest way (but not the only way) to solve this is to always put all the code for templates in the header (aka inline). See your favorite text book on template in C++ for more details.
template class TMyArray<0>;
template class TMyArray<1>;
template class TMyArray<2>;
.
.
.
At the bottom of the source (.cxx/cpp/cc…) file where TMyArray is defined. This has to be done for each template value for which you might want to use your class. In this way you can keep the declaration in the header and the definition in the source file.