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 toROOT::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 toXXX<int>::Streamer(TBuffer&)' x2.o: In function
XXX::IsA() const’:
x2.cxx:(.text._ZNK3XXXIdE3IsAEv[_ZNK3XXXIdE3IsAEv]+0xd): undefined reference toXXX<double>::Class()' x2.o: In function
XXX::ShowMembers(TMemberInspector&) const’:
x2.cxx:(.text._ZNK3XXXIdE11ShowMembersER16TMemberInspector[_ZNK3XXXIdE11ShowMembersER16TMemberInspector]+0x11): undefined reference toXXX<double>::Class()' x2.o: In function
XXX::IsA() const’:
x2.cxx:(.text._ZNK3XXXIiE3IsAEv[_ZNK3XXXIiE3IsAEv]+0xd): undefined reference toXXX<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