Add template class to ROOT error: class is not defined

Hi,

I wrote some simple codes to test how to add template class into ROOT. My codes are as follows:

//file A.h
#include "TObject.h"
template<typename T>
class A : public TObject{

 private:
  T x;
 
 public:
 
  A();
  ~A();
  ClassDef(A,1);

	};

// file A.cxx
#include "A.h" 
#if !defined(__CINT__)
templateClassImp(A);
#endif

A::A() { };
A::~A() {};

The error I got is

I tried to put all the code into one file A.cxx. And I still get the same error message. Any ideas? Thank for your helps.

regards,

gma

Hi,

Your function member implementation has C++ syntax errors. It should read:template <class T> A<T>::A() { }; template <class T> A<T>::~A() {};

Cheers,
Philippe

In addition since you inherit from TObject you must compile your code:root [] .L A.cxx+

Cheers,
Philippe.

Thanks, Philippe. :smiley: It works now. Another simple question is whether class T should be inherited from TObject. In my case, A is already inherited from TObject.

regards,

gma

Hi,

No. Your T does not have to inherit from TObject (unless you want it to). And actually neither does your templated class. However inheriting from TObject has it advantages (slightly faster I/O, can use TRef, can use TCollection(s)).

Cheers,
Philippe

Got it. Thanks,Philippe.

gma