Template classes witih nontype parameters

Hi folks,

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.


In TMyArray.h:

template <Int_t size>
class TMyArray {
  Int_t elements[size];



I have templateClassImp(TMyArray) in TMyArray.cpp. But what do I put in LinkDef.h? This fails:

#pragma link C++ class TMyArray<2>;

as does this:

#pragma link C++ class TMyArray;


This seems to be working in ROOT 5.08/00.
What error message are you getting?


Hi Phillipe,

If I say

#pragma link C++ class TMyArray;

then I get

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:
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.


Hi Philippe,

Well, I thought I did provide a constructor: lines 7-11 of TMyArray.cpp read as follows:

template <Int_t size>
 elements[0] = 1;

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.

ForPhilippe.tar (10 KB)

Hi Steve,

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.



Another way to do this is to include:

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.