Re: [ROOT] ROOT class with ifstream

From: Hajime Nanjyo (nanjyo@icepp.s.u-tokyo.ac.jp)
Date: Wed Jan 28 2004 - 05:02:36 MET


Dear Philippe,

Thanks a lot.
Your suggestion is really helpful and my class works fine
adding explicit copy constructor or private copy constructor
as you mentioned.

I usnderstand as follows.

The class ifstream is defined as 
'class ifstream : public fstreambase, public istream'
and istream is defined as
'class istream : virtual public ios'.

Then the class ios has private copy constructor as
class ios : public _ios_fields {
  ios& operator=(ios&);  /* Not allowed! */
  ios (const ios&); /* Not allowed! */
  public:
  .
  .
  .
};
.
So ifstream does not provide a copy constructor.

Best Regards,
Hajime

> Hi,
> 
> There is actually a C++ issue in your class:
> 
>   class  MyClass: public TObject
>   {
>   public:
>      MyClass();
>      ~MyClass();
>      std::ifstream fi;   //! excluded
>      ClassDef(MyClass,1) //MyClass
>   };
> 
> You class 'contains' an ifstream object and does not provide an
> explicit copy constructor.  In consequence the C++ compiler
> provide a default constructor and CINT assume its existence and
> refer to it.
> 
> You can get a different version of the error message by just
> doing:
> 	MyClass m;
> 	MyClass m2(m);
> 
> The problem here is that any std stream (derived from std::ios_base)
> is explicitly NOT copyable.  This is implemented by making the
> copy constructor of std::ios_base private and non-implemented.
> 
> For your MyClass, you __have to__ provide a copy constructor
> that resolved the issue of what it means for the ifstream.
> Or you can make your object non-copiable by adding:
> 
> 	private:
> 		MyClass(const MyClass&);
> 
> Cheers,
> Philippe.
> 
> -----Original Message-----
> From: owner-roottalk@pcroot.cern.ch
> [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Hajime Nanjyo
> Sent: Monday, January 26, 2004 9:07 PM
> To: roottalk@pcroot.cern.ch
> Subject: [ROOT] ROOT class with ifstream
> 
> 
> Dear Rooters,
> I forget the subject of the last mail
> for the ROOT class which have a member of ifstream.
> 
> Best Regards,
> Hajime
> 
> > Dear Rooters,
> >
> > I am suffering troubles when constructing my class
> > with ifstream for ROOT I/O.
> >
> > The shared library, libMyClass.so is successfully construced, but
> > it can't be linked to other objects and can't be loaded in ROOT(CINT).
> >
> > If the line, 'std::ifstream fi;   //! excluded'
> > is commented out (at MyClass.h), all works fine.
> >
> >
> > Is there any idea to solve the problem?
> >
> > /////////////////////////////////////////////////
> > The error outputs are...
> >
> > root [1] gSystem->Load("libMyClass")
> > dlopen error: /afs/cern.ch/user/n/nanjo/MyClassStreamer/./libMyClass.so:
> undefined symbol: _ZNSt8ios_baseC2ERKS_
> > Load Error: Failed to load Dynamic link library
> /afs/cern.ch/user/n/nanjo/MyClassStreamer/./libMyClass.so
> > *** Interpreter error recovered ***
> >
> > or
> >
> > g++ `root-config --cflags` -c MyClass.C
> > rootcint -f MyClassDict.C -c MyClass.h MyClassLinkDef.h
> > g++ `root-config --cflags` -c MyClassDict.C
> > g++ -shared MyClass.o MyClassDict.o -o  libMyClass.so
> > // Succeed
> > g++ `root-config --cflags` -c write.C
> > // OK
> > g++ write.o libMyClass.so `root-config --libs` -o write
> > // Fail
> > libMyClass.so: undefined reference to
> `std::ios_base::ios_base[not-in-charge](std::ios_base const&)'
> > collect2: ld returned 1 exit status
> >
> >
> > /////////////////////////////////////////////////
> > My environment is...
> >
> > uname -a
> > Red Hat Linux release 7.3 (Valhalla)
> > Linux lxplus072 2.4.20-28.7.cernsmp #1 SMP Fri Jan 9 12:37:16 CET 2004
> i686 unknown.
> >
> > g++ -v
> > Reading specs from
> /usr/local/gcc-alt-3.2/lib/gcc-lib/i686-pc-linux-gnu/3.2/specs
> > Configured with:
> ./configure --prefix=/usr/local/gcc-alt-3.2 --enable-shared
> > Thread model: posix
> > gcc version 3.2
> >
> > The ROOT is on afs at CERN.
> >   *******************************************
> >   *                                         *
> >   *        W E L C O M E  to  R O O T       *
> >   *                                         *
> >   *   Version   3.10/02  16 December 2003   *
> >   *                                         *
> >   *  You are welcome to visit our Web site  *
> >   *          http://root.cern.ch            *
> >   *                                         *
> >   *******************************************
> > ///////////////////////////////////////////////////
> >
> >
> >
> >
> > My codes are as follows.
> >
> > ///////////////////////////////////////////////////
> > //
> > //  MyClass.h
> > //
> > #ifndef MyClass_HH
> > #define MyClass_HH
> > #include <fstream>
> >
> > #include "TObject.h"
> >
> > class  MyClass: public TObject
> > {
> >  public:
> >   MyClass();
> >   ~MyClass();
> >   void Set(int s);
> >
> >  public:
> >   int      size;
> >   std::ifstream fi;   //! excluded
> >   ClassDef(MyClass,1) //MyClass
> > };
> >
> > #endif
> > ///////////////////////////////////////////////////
> > ///////////////////////////////////////////////////
> > ///////////////////////////////////////////////////
> > //
> > // MyClass.C
> > //
> > #include "MyClass.h"
> > #if !defined(__CINT__)
> > ClassImp(MyClass);
> > #endif
> > MyClass::MyClass() {
> >   size=0;
> > }
> >
> > MyClass::~MyClass() {
> >   ;
> > }
> >
> >
> > void MyClass::Set(int s) {
> >   size=s;
> > }
> >
> > ///////////////////////////////////////////////////
> > //
> > // hadDataLinkDef.h
> > //
> > #ifdef __CINT__
> > #pragma link off all globals;
> > #pragma link off all classes;
> > #pragma link off all functions;
> > #pragma link C++ class MyClass;
> > #endif
> > ///////////////////////////////////////////////////
> > ///////////////////////////////////////////////////
> > ///////////////////////////////////////////////////
> > //
> > // write.C
> > //
> > #include "TApplication.h"
> > #include "TFile.h"
> > #include "TTree.h"
> > #include "MyClass.h"
> >
> > int main(int argc,char** argv)
> > {
> >   TApplication app("app", &argc, argv);
> >   TFile *tf = new TFile("tmp.root","RECREATE");
> >   TTree *tree = new TTree("tree","tmp");
> >
> >   MyClass *data = new MyClass();
> >   tree->Branch("data","MyClass",&data);
> >
> >   for (int i=0;i<5;++i) {
> >     data->Set(i+1);
> >     tree->Fill();
> >   }
> >
> >   tf->Write();
> >   delete data;
> >
> >   return 0;
> > }
> > ///////////////////////////////////////////////////
> > ///////////////////////////////////////////////////
> > ///////////////////////////////////////////////////
> > //
> > // compile script
> > //
> > rm *.o
> > rm *.so
> > rm MyClassDict.C
> > g++ `root-config --cflags` -c MyClass.C
> > rootcint -f MyClassDict.C -c MyClass.h MyClassLinkDef.h
> > g++ `root-config --cflags` -c MyClassDict.C
> > g++ -shared MyClass.o MyClassDict.o -o  libMyClass.so
> > g++ `root-config --cflags` -c write.C
> > g++ write.o libMyClass.so `root-config --libs` -o write
> > ///////////////////////////////////////////////////



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:05 MET