RE:#pragma problems for function: ostream &

From: Masaharu Goto (MXJ02154@nifty.ne.jp)
Date: Wed May 26 1999 - 14:39:42 MEST


Maurik,

The problem is simply because iostream.h is not loaded at that time.
When you interpret the code, iostream.h is loaded by #include statement
in test2.h.  When you compile test2.C and use test2.so,  nobody loads
iostream.h.  #include statement in test2.h , in this case, does not reach
to the interpreter.

Our recommentation is to load iostream.h in rootlogon.C. Please add following
statement in rootlogin.C. This will solve the problem. (Rene, Is this already
in ROOT tutorial?)

   G__loadfile("iostream.h");


BTW,
When you have doubt or trouble with #pragma link statement, there is a simple
way to check it. From ROOT prompt, try ".func" , ".class" and ".class tempa"
command.

   root[0] .L test2.so
   root[1] .func
      ### list of loaded functions ###
   root[2] .class
      ### list of loaded classes ###
   root[3] .class tempa
      ### list of tempa class contents ###

Best Regard,
Masaharu Goto

==========================================================================
Dear Root,

I have a little example code that works great when interpreted, but when
compiled does not function properly. I suspect an incorrect LinkDef.h file.
I tried a whole slew of different #pragma link C++ function combinations
without much results. Could someone please help me find a way to get access
to the ostream &operator<<() functions?

Maurik

------------------------------------------------------------------------------
-
-

Interpreted output:  (Note: to make the interpreter work properly I comment
out the ClassDef and ClassImp)

root [0] .L test2.cc
root [1] tempa aaa(1,1.234);
root [2] cout << aaa;
Int: 1  Float: 1.234  C: Initialized with constructor.

Compiled output:

server|root>g++ -c -DEGCS -fno-rtti -O -Wall -fPIC -DLinux -DRedHat5
-I/usr/local/root/include test2.cc
server|root>rootcint -f test2cint.cc -c test2.h test2_LinkDef.h
Note: operator new() masked 1c
Note: operator delete() masked 1c
server|root>g++ -c -DEGCS -fno-rtti -O -Wall -fPIC -DLinux -DRedHat5
-I/usr/local/root/include test2cint.cc
server|root>g++ -shared test2.o test2cint.o -o test2.so
server|root>
server|root>
server|root>root
CINT/ROOT C/C++ Interpreter version 5.13.83, Dec 2 1998
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .L test2.so
root [1] tempa aaa(1,1.234);
root [2] cout << aaa;
Error: No symbol cout in current scope  FILE:/tmp/09439baa LINE:1
(class tempa)139247968
*** Interpreter error recovered ***
root [3]

---------------Code: test2.h -------------------
#ifndef __CINT__
#include "TObject.h"
#endif

#include <iostream.h>


class tempa : public TObject{

public:
  int i;
  float x;
  char str[256];

public:

  tempa(){};
  ~tempa(){};
  tempa(int a,float b){
    i=a;
    x=b;
    strcpy(str,"Initialized with constructor.");
  }

  void Print(){
    cout << (*this) << endl;
  }

  friend ostream &operator<<(ostream &os,tempa &b);

  ClassDef(tempa,1)
};

---------------- Code: test2.cc ------------------------
#include "test2.h"

ClassImp(tempa)

ostream &operator<<(ostream &os,tempa &b)
{
  os << "Int: " << b.i;
  os << "  Float: " << b.x;
  os << "  C: " << b.str << endl;
  return(os);
}
--------------- Code: test2LinkDef.h ---------------------
#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;

#pragma link C++ class tempa;
#pragma link C++ function operator<<(ostream &, class tempa &);
#endif

--------------------------------------------------



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:33 MET