User defined class inside root

I have been following the documentation [root.cern.ch/download/doc/AddingaClass.html#dictionaries-for-stl] for adding a class, but I can’t get it to compile successfully. I get an undefined reference… error.
Thanks in advance.

Header file Sclass.h

[code]#ifndef SCLASS_H
#define SCLASS_H

#include
#include "TObject.h"
class SClass : public TObject {
private:
Float_t fX; //x position in centimeters
Float_t fY; //y position in centimeters
Int_t fTempValue; //! temporary state value

public:
SClass() { fX = fY = -1; }
void Print() const;
void SetX(float x) { fX = x; }
void SetY(float y) { fY = y; }

ClassDef(SClass, 1)
};

#endif
[/code]

Implementation SClass.cxx

#include "SClass.h" ClassImp(SClass); void SClass::Print() const { cout << "fX = " << fX << ", fY = " << fY << endl; }

using the definitions : usingSClass.C

[code]#include “SClass.h”
//#include “mydict.h”
#include

using namespace std;

void usingSClass(){

    SClass s;                                               
    s.SetX(2);                                              
    s.SetY(2);                                              
    s.Print();                                              

} [/code]

at Root prompt
root[0] : .L usingSClass.C++

root [10] .L usingSClass.C++
Info in TUnixSystem::ACLiC: creating shared library /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./usingSClass_C.so
In file included from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/usingSClass_C_ACLiC_dict.h:32:0,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/usingSClass_C_ACLiC_dict.cxx:17:
/usr/include/root/TObject.h:144:24: warning: ‘virtual void TObject::Print(Option_t*) const’ was hidden [-Woverloaded-virtual]
In file included from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./usingSClass.C:1:0,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/usingSClass_C_ACLiC_dict.h:34,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/usingSClass_C_ACLiC_dict.cxx:17:
/home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass.h:14:12: warning: by ‘void SClass::Print() const’ [-Woverloaded-virtual]
/home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/usingSClass_C_ACLiC_dict.o: In function SClass': /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass.h:13: [b]undefined reference tovtable for SClass’[/b]
/home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/usingSClass_C_ACLiC_dict.o: In function usingSClass()': /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./usingSClass.C:12: [b]undefined reference toSClass::Print() const’[/b]
collect2: error: ld returned 1 exit status
Error in : Compilation failed!

root [0] .L SClass.cxx++
root [1] .L usingSClass.C++

Hello Coyote,

Here’s the output based on your suggestions…

root [7] .L SClass.cxx++
Info in TUnixSystem::ACLiC: creating shared library /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass_cxx.so
In file included from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.h:32:0,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.cxx:17:
/usr/include/root/TObject.h:144:24: warning: ‘virtual void TObject::Print(Option_t*) const’ was hidden [-Woverloaded-virtual]
In file included from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass.cxx:1:0,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.h:34,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.cxx:17:
/home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass.h:14:12: warning: by ‘void SClass::Print() const’ [-Woverloaded-virtual]
In file included from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.h:34:0,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.cxx:17:
/home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass.cxx:3:6: error: prototype for ‘void SClass::Print()’ does not match any in class ‘SClass’
In file included from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass.cxx:1:0,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.h:34,
from /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.cxx:17:
/home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass.h:14:12: error: candidate is: void SClass::Print() const
g++: error: /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/SClass_cxx_ACLiC_dict.o: No such file or directory
Error in : Compilation failed!

I have another question - what can do with the dictionary that I’ve created ?
mydict.cxx, SClass.h obtained from rootcint mydict.cxx -c SClass.h

Try to rename your “SClass::Print” method into “SClass::MyPrint” (in order to get rid of the clash with the “TObject::Print” method), or change your: void Print() const into: void Print(Option_t *option = "") const.
You can compile “mydict.cxx” and then create a shared library out of “SClass.o” and “mydict.o” (which provides the dictionary for your “SClass”).

It worked, thanks a ton Coyote. :smiley: Yes, the Print method clashes, but I also got errors after changing it to myPrint(), that’s because I need to add const in the declaration i.e SClass.cxx

Could you please show the commands how I can use the dictionary that’s generated?

SClass.cxx

#include "SClass.h" ClassImp(SClass); void SClass::myPrint() const { cout << "fX = " << fX << ", fY = " << fY << endl; }

SClass.h

[code]#ifndef SCLASS_H
#define SCLASS_H

#include
#include "TObject.h"
class SClass : public TObject {
private:
Float_t fX; //x position in centimeters
Float_t fY; //y position in centimeters
Int_t fTempValue; //! temporary state value

public:
SClass(){ fX =-1; fY = -1; }
void myPrint() const;
void SetX(float x) { fX = x; }
void SetY(float y) { fY = y; }

ClassDef(SClass, 1)
};

#endif [/code]

usingSClass.C

[code]#include “SClass.h”
//#include “mydict.h”
#include

using namespace std;

void usingSClass(){

    SClass s;
    s.SetX(2);
    s.SetY(2);
    s.myPrint();

}[/code]

To Run :
— Compiling the Definition to produce the shared library .so —
root [1] .L SClass.cxx+
Info in TUnixSystem::ACLiC: creating shared library /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./SClass_cxx.so

— Executing the code using the Class defined outside root ----
root [2] .L usingSClass.C++
Info in TUnixSystem::ACLiC: creating shared library /home/baryon/Simulations/Simulations/HIJING_v2/AddingUserClassRoot/./usingSClass_C.so

—Output—
root [3] usingSClass()
fX = 2, fY = 2

See, for example, the quite old “[url=https://root-forum.cern.ch/t/trying-to-compile-rootcint-dictionary-with-namespaces/14562/1 to compile rootcint dictionary with namespaces[/url]” and “[url=https://root-forum.cern.ch/t/adding-a-class-hand-holding-requested/13893/1 a class: hand holding requested[/url]” threads.