Problems with a template class inheriting from TObject

Hi all,
I’d like to replace some of my classes explicitly operating on int and double types by a template class operating on type T. First I wrote a simple class without any dependance on ROOT:

#include <vector>
template <class T>
class XXX {
public:
XXX(){};
XXX(int Size, T InitVal = 0) { if (Size > 0) fArray.assign(Size, InitVal); };
inline T & operator(int Index) { return fArray[Index]; }
protected:
vector<T> fArray;
};

The main program instantiates XXX a(100) and XXX b(100). Works fine.

x1.cxx:
include “classXXX1.h”
#include <iostream>
int main() {
XXX<int> a(100);
XXX<double> b(100);
cout << a[50] << " " << b[50] << endl;
}

As soon as I add the inheritance on TObject I run into problems:

#include <vector>
#include “TObject.h”
template <class T>
class XXX : public TObject {
public:
XXX(){};
XXX(int Size = 0, T InitVal = 0) { if (Size > 0) fArray.assign(Size, InitVal); };
inline T & operator(int Index) { return fArray[Index]; }
protected:
vector<T> fArray;
ClassDef(XXX, 1);
};

x2.cxx:
#include “classXXX2.h”
#include <iostream>
ClassImp(XXX<int>)
ClassImp(XXX<double>)
int main() {
XXX<int> a(100);
XXX<double> b(200);
cout << a[50] << " " << b[50] << endl;
}

Loading it together with root libs (“root-config --libs”) gives a long list of errors:

g++ x2.o root-config --libs -o x2
x2.o: In function __static_initialization_and_destruction_0(int, int)': x2.cxx:(.text+0x1ee): undefined reference to ROOT::GenerateInitInstance(XXX const*)’
x2.cxx:(.text+0x212): undefined reference to ROOT::GenerateInitInstance(XXX<double> const*)' x2.o:(.data.rel.ro._ZTV3XXXIdE[_ZTV3XXXIdE]+0x1e0): undefined reference to XXX::Streamer(TBuffer&)’
x2.o:(.data.rel.ro._ZTV3XXXIiE[_ZTV3XXXIiE]+0x1e0): undefined reference to XXX<int>::Streamer(TBuffer&)' x2.o: In function XXX::IsA() const’:
x2.cxx:(.text._ZNK3XXXIdE3IsAEv[_ZNK3XXXIdE3IsAEv]+0xd): undefined reference to XXX<double>::Class()' x2.o: In function XXX::ShowMembers(TMemberInspector&) const’:
x2.cxx:(.text._ZNK3XXXIdE11ShowMembersER16TMemberInspector[_ZNK3XXXIdE11ShowMembersER16TMemberInspector]+0x11): undefined reference to XXX<double>::Class()' x2.o: In function XXX::IsA() const’:
x2.cxx:(.text._ZNK3XXXIiE3IsAEv[_ZNK3XXXIiE3IsAEv]+0xd): undefined reference to XXX<int>::Class()' x2.o: In function XXX::ShowMembers(TMemberInspector&) const’:
x2.cxx:(.text._ZNK3XXXIiE11ShowMembersER16TMemberInspector[_ZNK3XXXIiE11ShowMembersER16TMemberInspector]+0x11): undefined reference to `XXX::Class()’
collect2: error: ld returned 1 exit status

What’s going wrong?

Cheers,
Rudi

You need to generate and compile and link with the dictionary for your classes. (see rootcling and LinkDef files)

Hi Philippe,
I was pretty sure that I did it that way in a second try. I generated the dictionary via rootcling, compiled it and linked it. But the errors remained. Now I repeated these steps - and it works. The real template classes in my library work too. Sorry for the inconvenience …
Rudi

A final comment:
The errors were due to the fact that I included in Linkdef.h the pure name of the templated class “XXX” instead of including the different flavors XXX<int>, XXX<double>.