Re: [ROOT] Problems with TString and TMatrix in class member functions

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Jan 04 2001 - 17:50:46 MET


Hi Christian,


On Thu, 4 Jan 2001 cstrato@EUnet.at wrote:

> Dear Rene
> 
> Thank you very much for answering my questions. Since you said, you do not
> understand my second question regarding TMatrix without having a working example,
> I have implemented a "TestClass" which I compile as library "TestClass.so"
> and use in macro "MacroTestClass.C.
> Below I have attached all necessary files.
> 
> When I write a simple macro (see TestMacro.C below), all works fine, but
> when I put the same code in a member function I cannot compile the library.
> However, when I put TMatrix on the stack and comment out the TString code,
> everything works fine.
> 
> I have the following questions:
> 
> 1, Why can I not use vMatrix(i,j) when TMatrix is created on the heap?
>    I get the following error when compiling TestClass:

That is the way the operator () is defined. Do:
   TMatrix *vM = new TMatrix(vNRows,vNCols);
   TMatrix &vMatrix = *vM;

> 
> [christian@cookiebook rootcode]$ gmake -f Makefile4TestClass
> g++ -O -Wall -fPIC -fsigned-char -I/usr/X11/include -I/opt/root/include -c TestClass.cxx
> TestClass.cxx: In method `void TestClass::FTest2(int, int)':
> TestClass.cxx:59: `vMatrix' cannot be used as a function
> TestClass.cxx:62: `vMatrix' cannot be used as a function
> TestClass.cxx:63: `vMatrix' cannot be used as a function
> gmake: *** [TestClass.o] Error 1
> 
> 2, Why can I not append a number to TString?
>    I get the following error when compiling TestClass:
> 

This problem was fixed in TString two weeks ago. You can import the
current CVS version.

Rene Brun

> [christian@cookiebook rootcode]$ gmake -f
Makefile4TestClass
> g++ -O -Wall -fPIC -fsigned-char -I/usr/X11/include -I/opt/root/include -c TestClass.cxx
> TestClass.cxx: In method `void TestClass::FTest1(const char *)':
> TestClass.cxx:40: ambiguous overload for `TString & += Int_t &'
> /opt/root/include/TString.h:384: candidates are: class TString & TString::operator +=(const
> TString &)
> /opt/root/include/TString.h:387:                 class TString & TString::operator +=(char)
> 
> /opt/root/include/TString.h:390:                 class TString & TString::operator +=(long
> int)
> /opt/root/include/TString.h:393:                 class TString & TString::operator +=(long
> unsigned int)
> gmake: *** [TestClass.o] Error 1
> 
> Thank you once again for your help.
> 
> Best regards
> Christian Stratowa
> Vienna, Austria
> 
> My system: PowerBook running LinuxPPC 2000
>            root 3.00/00  19 December 2000
> 
> //--------------------------------------
> // TestMacro.C
> //--------------------------------------
> {
> // test: append to string
>    TString s = "Test";
>    for (Int_t i=0;i<4;i++) {
>       s = "Test";
>       s+=i;
>       s+=".";
>       cout << s << endl;
>    }
> 
>    Int_t vNRows = 3;
>    Int_t vNCols = 4;
> // test: matrix on heap
>    TMatrix *vMatrix1 = new TMatrix(vNRows,vNCols);
>    for (Int_t i=0;i<vNRows;i++) {
>       for (Int_t j=0;j<vNCols;j++) {
>          vMatrix1(i,j) = i + j;
>       }
>    }
>    cout << "Matrix1(0,0) = " << vMatrix1(0,0) << endl;
>    cout << "Matrix1(NRows-1,NCols-1) = " << vMatrix1(vNRows-1,vNCols-1) << endl;
>    delete vMatrix1;
> 
> // test: matrix on stack
>    TMatrix vMatrix2(vNRows,vNCols);
>    for (Int_t i=0;i<vNRows;i++) {
>       for (Int_t j=0;j<vNCols;j++) {
>          vMatrix2(i,j) = i + j;
>       }
>    }
>    cout << "Matrix2(0,0) = " << vMatrix2(0,0) << endl;
>    cout << "Matrix2(NRows-1,NCols-1) = " << vMatrix2(vNRows-1,vNCols-1) << endl;
> }//macro
> 
> 
> //--------------------------------------
> // MacroTestClass.C
> //--------------------------------------
> {
>    gROOT->Reset();
>    gSystem->Load("/opt/rootcode/TestClass.so");
> 
>    TestClass *vTestClass = new TestClass("Test",1.2,1,2,3);
> 
>    vTestClass->FTest1("Test");
>    vTestClass->FTest2(3,4);
> 
>    delete vTestClass;
> }//macro
> 
> 
> //--------------------------------------
> // TestClass.h
> //--------------------------------------
> #ifndef __TestClass__
> #define __TestClass__
> 
> #include <iostream.h>
> #include "TObject.h"
> #include "TString.h"
> 
> class TestClass: public TObject {
> 
>    private:
>       TString       fText;
>       Float_t       fFloat;
>       Int_t         fInt;
>       Int_t         fX;
>       Int_t         fY;
> 
>    public :
> 
>       TestClass();
>       TestClass(TString vText, Float_t vFloat, Int_t vInt, Int_t x, Int_t y);
>       ~TestClass();
> 
>       void FTest1(const char *vText);
>       void FTest2(Int_t vNRows,Int_t vNCols);
> 
>       void FSetFloat(Float_t vFloat) {fFloat = vFloat;}
>       void FSetInt(Int_t vInt) {fInt = vInt;}
>       void FSetX(Int_t vX) {fX = vX;}
>       void FSetY(Int_t vY) {fY = vY;}
> 
>       Float_t FGetFloat() const {return fFloat;}
>       Int_t FGetInt() const {return fInt;}
>       Int_t FGetX() const {return fX;}
>       Int_t FGetY() const {return fY;}
> 
>       ClassDef(TestClass,1) //TestClass
> };
> 
> #endif
> 
> 
> //--------------------------------------
> // TestClass.cxx
> //--------------------------------------
> #include "TestClass.h"
> #include "TMatrix.h"
> 
> ClassImp(TestClass);
> 
> TestClass::TestClass(): TObject()
> {
>    cout << "TestClass::TestClass" << endl;
> }
> 
> TestClass::TestClass(TString vText, Float_t vFloat, Int_t vInt, Int_t x, Int_t y):
> TObject()
> {
>    cout << "TestClass::TestClass" << endl;
> 
>    fText = vText;
>    fFloat = vFloat;
>    fInt = vInt;
>    fX = x;
>    fY = y;
> }//Constructor
> 
> TestClass::~TestClass()
> {
>    cout << "~TestClass::TestClass" << endl;
> }//Destructor
> 
> void TestClass::FTest1(const char *vText)
> {
>    cout << "TestClass::FTest1" << endl;
> 
> // test: append to string
>    TString s = vText;
> //* cannot compile
>    for (Int_t i=0;i<4;i++) {
>       s = "xxx";
>       s+=i;
>       s+=".";
>       cout << s << endl;
>    }//for
> //*/
>    cout << "String = " << s << endl;
> }//FTest1
> 
> void TestClass::FTest2(Int_t vNRows,Int_t vNCols)
> {
>    cout << "TestClass::FTest2" << endl;
> 
> // test: fill matrix
> //   TMatrix *vMatrix = new TMatrix(vNRows,vNCols);
>    TMatrix vMatrix(vNRows,vNCols);
> 
>    for (Int_t i=0;i<vNRows;i++) {
>       for (Int_t j=0;j<vNCols;j++) {
>          vMatrix(i,j) = i + j;
>       }
>    }
>    cout << "Matrix(0,0) = " << vMatrix(0,0) << endl;
>    cout << "Matrix(NRows-1,NCols-1) = " << vMatrix(vNRows-1,vNCols-1) << endl;
> 
> //   delete vMatrix;
> }//FTest2
> 
> 
> //--------------------------------------
> // TestClassLinkDef.h
> //--------------------------------------
> #ifdef __CINT__
> 
> #pragma link off all globals;
> #pragma link off all classes;
> #pragma link off all functions;
> 
> #pragma link C++ class TestClass+;
> 
> #endif
> 
> 
> //--------------------------------------
> // Makefile4TestClass
> //--------------------------------------
> # Makefile for class TestClass.
> # shell: gmake -f Makefile4TestClass
> 
> ARCH          = linuxppcegcs
> 
> CXX           =
> ObjSuf        = o
> SrcSuf        = cxx
> ExeSuf        =
> DllSuf        = so
> OutPutOpt     = -o
> 
> ROOTCFLAGS   := $(shell root-config --cflags)
> ROOTLIBS     := $(shell root-config --libs)
> ROOTGLIBS    := $(shell root-config --glibs)
> 
> 
> ifeq ($(ARCH),linuxegcs)
> # Linux with egcs (>= RedHat 5.2)
> CXX           = g++
> CXXFLAGS      = -O -Wall -fPIC
> LD            = g++
> LDFLAGS       = -O
> SOFLAGS       = -shared
> endif
> 
> ifeq ($(ARCH),linuxppcegcs)
> # MkLinux with egcs/glibc
> CXX           = g++
> CXXFLAGS      = -O -Wall -fPIC
> LD            = g++
> LDFLAGS       = -O
> SOFLAGS       = -shared -Wl,-soname,
> endif
> 
> 
> CXXFLAGS     += $(ROOTCFLAGS)
> LIBS          = $(ROOTLIBS) $(SYSLIBS)
> GLIBS         = $(ROOTGLIBS) $(SYSLIBS)
> 
> #------------------------------------------------------------------------------
> 
> TESTCLASSO       = TestClass.$(ObjSuf) TestClassDict.$(ObjSuf)
> TESTCLASSS       = TestClass.$(SrcSuf) TestClassDict.$(SrcSuf)
> TESTCLASSSO      = TestClass.$(DllSuf)
> 
> 
> OBJS          = $(TESTCLASSO)
> PROGRAMS      = $(TESTCLASSSO)
> 
> #------------------------------------------------------------------------------
> 
> .SUFFIXES: .$(SrcSuf) .$(ObjSuf) .$(DllSuf)
> 
> all:            $(PROGRAMS)
> 
> $(TESTCLASSSO):    $(TESTCLASSO)
>                 $(LD) $(SOFLAGS) $(LDFLAGS) $^ $(OutPutOpt) $@
> 
> clean:
>   @rm -f $(OBJS) core
> 
> .SUFFIXES: .$(SrcSuf)
> 
> ###
> 
> 
> TestClass.$(ObjSuf): TestClass.h
> TestClassDict.$(SrcSuf): TestClass.h TestClassLinkDef.h
>  @echo "Generating dictionary TestClassDict..."
>  @rootcint -f TestClassDict.$(SrcSuf) -c TestClass.h TestClassLinkDef.h
> 
> .$(SrcSuf).$(ObjSuf):
>  $(CXX) $(CXXFLAGS) -c $<
> 
> #-------------------------------------------
> 
> 
> Rene Brun wrote:
> 
> > Hi Christian,
> >
> > > 3, Why can I not use vMatrix(x,y) when TMatrix is created on the heap?
> >
> > I do not understand this point without having a working example.
> >
> > Rene Brun
> >
> 



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:33 MET