[gnu.g++] Stream Manipulators: A Question

dsouza@mcc.com (02/10/90)

A question about stream manipulators:
	e.g.  cout << "Enter a Number: " << endl ;
described in Lippman, p. 378.

"endl" is a manipulator: i.e. has some side effects other than simply
printing out something -- in this case, print new-line and flush the
stream. It is supposed to be implemented as a function which takes an
ostream& as an single argument does arbitrary things and returns an
ostream&. Manipulators which require additional arguments are more
complicated.

According to the AT&T C++ 2.0 Library Manual (p 3-18) the following
should be sufficient to define a simple manipulator called "tab". SUN C++
accepts it, g++ 1.36.4 wont compile it, complaining it "cannot resolve
overloaded function `tab' based on non-function type". 

1. What does this error message mean ?
2. Is this a bug in g++, or is it not a part of the 2.0 language
   specification?
Thanks
Desmond.

//--------------------- begin -------------
#include<stream.h>

ostream& tab(ostream& o) {
  return o << "\t"; }

main () {
  cout << 1 << tab << 2 << tab << 3 ;
}
//--------------------- end -------------

Here is what g++ does:

optima% g++ -v manip.cc
g++ version 1.36.4 (based on GCC 1.36.93)
 /usr/local/gnu/1.36.4/lib/gcc-cpp -+ -v -undef -D__GNUC__ -D__GNUG__ -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ manip.cc /usr/tmp/cca17706.cpp
GNU CPP version 1.36.92
 /usr/local/gnu/1.36.4/lib/gcc-cc1plus /usr/tmp/cca17706.cpp -quiet -dumpbase manip.cc -g -version -o /usr/tmp/cca17706.s
GNU C++ version 1.36.4 (based on GCC 1.36.93) (sparc) compiled by GNU C version 1.36.92.
default target switches: -mfpu -mepilogue

manip.cc: In function int main ():
manip.cc:8: cannot resolve overloaded function `tab' based on non-function type
manip.cc:8: type conversion required for type `ostream'
optima% 


And here is SUN C++:

optima% CC manip.cc
CC  manip.cc:
cc   -I/net/sunspot/usr/cadsw/CC/sun4/incl  manip.c  -L/net/sunspot/usr/cadsw/CC/sun4/ -lC
optima% 
optima% a.out
1	2	3
optima% 

-------------------------------------------------------------------------------
 Desmond D'Souza, MCC CAD Program | ARPA: dsouza@mcc.com | Phone: [512] 338-3324
 Box 200195, Austin, TX 78720 | UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!dsouza

rich@Rice.edu (Rich Murphey) (02/11/90)

Desmond D'Souza asks about stream manipulators:

   According to the AT&T C++ 2.0 Library Manual (p 3-18) the following
   should be sufficient to define a simple manipulator called "tab". SUN C++
   accepts it, g++ 1.36.4 wont compile it, complaining it "cannot resolve
   overloaded function `tab' based on non-function type". 

   //--------------------- begin -------------
   #include<stream.h>

   ostream& tab(ostream& o) {
   return o << "\t"; }

   main () {
   cout << 1 << tab << 2 << tab << 3 ;
   }
   //--------------------- end -------------

Perhaps this is naive, but the definition of tab as a function and
it's use as a variable are confusing.

It seems like you want to create an object which prints a tab when
output to a stream.  One solutiong to your problem in g++ might be:

#include <stream.h>
class Tab
{
public:
  friend ostream& operator << (ostream&, const Tab&);
};
inline friend ostream& operator << (ostream& out, const Tab& p)
{
  return out << "\t";
}
Tab tab;

main ()
{
  cout << 1 << tab << 2 << tab << 3 ;
}

Hope that helps,
--
Rich@rice.edu