Creating a class with templated functions

Hi ROOTers,

I’m having trouble with a probably-trivial LinkDef file. I think that if you could help me with the following simplified class, I could take it from there.

The class is declared and defined in MyClass.h:

// MyClass.h
#include "TObject.h"

class MyClass : public TObject
{
  public:
    MyClass() : TObject(), fInt(0) {}
    template <typename T>
    MyClass(T input) : TObject() { fInt = static_cast<Int_t>(input); }
    template <typename T>
    void SetInt(T input) { fInt = static_cast<Int_t>(input); }
    Int_t fInt;
  ClassDef(MyClass, 1)
};
ClassImp(MyClass)

The LinkDef file I wrote for this (where, for the sake of simplicity, I figure input will be just Int_t and Double_t) is:

// LinkDef.h
#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ nestedclasses;

#pragma link C++ class MyClass;

#pragma link C++ function MyClass::MyClass(Int_t);
#pragma link C++ function MyClass::MyClass(Double_t);

#pragma link C++ function MyClass::SetInt(Int_t);
#pragma link C++ function MyClass::SetInt(Double_t);
#endif

I try to create a library for this object, and get:

$ rootcint -f Dict.C -c -I`root-config --incdir` MyClass.h
$ g++  `root-config --cflags` `root-config --ldflags` -fPIC -shared -o Dict.so Dict.C
In file included from Dict.h:34:0,
                 from Dict.C:17:
MyClass.h: In constructor ‘MyClass::MyClass(T) [with T = TRootIOCtor*]’:
Dict.C:138:53:   instantiated from here
MyClass.h:8:36: error: invalid static_cast from type ‘TRootIOCtor*’ to type ‘Int_t’

When I remove the templated constructor from MyClass.h and LinkDef.h, the code compiles; however, attempting to use the SetInt function, I get:

root [0] gSystem->Load("Dict.so")
(int)0
root [1] MyClass c
root [2] Int_t i = 2
root [3] Double_t d = 4.5
root [4] c.SetInt(i)
Error: Can't call MyClass::SetInt(i) in current scope (tmpfile):1:
Possible candidates are...
(in MyClass)
*** Interpreter error recovered ***
root [5] c.SetInt(d)
Error: Can't call MyClass::SetInt(d) in current scope (tmpfile):1:
Possible candidates are...
(in MyClass)
*** Interpreter error recovered ***

Any help on this would be greatly appreciated!

Thanks,
Clayton

PS. I am using the v5-28-00-patches branch of ROOT, revision 39785 (June 16, 2011).

An update for anyone browsing this forum posting later:

Regarding the problem with the templated member function, that seems to have just been a mistake with my rootcint invokation. The commands should have been:

$ rootcint -f Dict.C -c -I`root-config --incdir` MyClass.h LinkDef.h
$ g++  `root-config --cflags` `root-config --ldflags` -fPIC -shared -o Dict.so Dict.C MyClass.h

So, I forgot to add LinkDef.h to the rootcint command, and I added the class definitions to the compilation step (since the body of the class is included from Dict.C, not copied).

For the problem where I couldn’t include a templated constructor, I’ve filed a bug report:
https://savannah.cern.ch/bugs/?86195