C++ header for 'push_back'

1998-10-30 Thread Bob Bernstein
I'm posting this here because I suspect my question may concern the c++ setup
on this Debian 2.0 install I have here. I'm trying to compile a sample snippet
of code (from the STL docs at SGI) and I can't find the right header to
declare push_back:

//my headers --
#include algorithm // needed for 'reverse' (is this correct?)
#include iomanip
#include string

//--- here's the code snippet ---
int main() { 
  string s(10u, ' ');   // Create a string of ten blanks.

  const char* A = this is a test;
  s += A;
  cout  s =   (s + '\n');
  cout  As a null-terminated sequence:   s.c_str()  endl;
  cout  The sixteenth character is   s[15]  endl;
  
  reverse(s.begin(), s.end());
  s.push_back('\n');
  cout  s;
}

The compile complains that:

/home/bernie/cpp/stl.cpp: In function `int main()':
/home/bernie/cpp/stl.cpp:17: no matching function for call to
`basic_stringchar,string_char_traitschar,__default_alloc_templatetrue,0

with line 17 being the s.push_back call.

I'm using the egcs setup that came with the default install of 2.0. I upgraded
some deb packages, so this is what I have now:

 egcc2.90.29-0.6The GNU (egcs) C compiler.
 libstdc++2.82.90.29-0.6The GNU stdc++ library (egcs version)
 libstdc++2.8-de 2.90.29-0.6The GNU stdc++ library (development files)


Any clues, anyone, that might help this c++ neophyte gain some ground here?

TIA for any light shed on these wonderful mysteries!


--
Bob Bernstein
at
Esmond, R.I., USA[EMAIL PROTECTED]  http://www.brainiac.com/bernie


Re: C++ header for 'push_back'

1998-10-30 Thread Havoc Pennington

On Fri, 30 Oct 1998, Bob Bernstein wrote:
   
   reverse(s.begin(), s.end());
   s.push_back('\n');
   cout  s;
 }
 
 The compile complains that:
 
 /home/bernie/cpp/stl.cpp: In function `int main()':
 /home/bernie/cpp/stl.cpp:17: no matching function for call to
 `basic_stringchar,string_char_traitschar,__default_alloc_templatetrue,0


I assume the rest of the error message says ::push_back(char)?
 
All the methods for string are in the basic_string class declaration; if
push_back()  isn't there, there is no push_back() implementation for
string.  Conveniently C++ requires the entire class to be declared in one
place, so it's easy to see if a method exists. 

Stroustrup (page 593, 3rd edition) says string does not have push_back().
Try:

 s += '\n';

instead.

You can see the basic_string class in /usr/include/g++/std/bastring.h;
string is a typedef (typedef basic_string char string) which is in the
string header. basic_string can also use wide characters (like Unicode,
I guess), that's why it's a template.

Havoc





Re[2]: C++ header for 'push_back'

1998-10-30 Thread Bob Bernstein
Havoc Pennington [EMAIL PROTECTED] wrote:

 All the methods for string are in the basic_string class declaration; if
 push_back()  isn't there, there is no push_back() implementation for
 string.

So I've concluded this afternoon! I took a snippet off one of the newsgroups
that demonstrated push_back for a *vector* of strings, and tried to get it to
work for an array (named array oddly enough) of strings; the error message
clued me in:

/home/bernie/cpp/vector2.cpp: In function `int main()':
/home/bernie/cpp/vector2.cpp:30: request for member `push_back' in `array',
which is of non-aggregate type
`basic_stringchar,string_char_traitschar,__default_alloc_templatetrue,0

 Stroustrup (page 593, 3rd edition) says string does not have push_back().

That's weird, my copy (same edition) says, Because string has a
push_back()... but in context it's clear that that must be a typo fixed in a
later printing. Mine is 5th printing, Jan, 1998. (?)

Now I wonder why that snip is in those STL docs? They are purported to be the
authoritative 'latest stuff', are they not? Oh well, there's a controversy for
another day.

Thanks!


--
Bob Bernstein
at
Esmond, R.I., USA[EMAIL PROTECTED]  http://www.brainiac.com/bernie