This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: g++/stl bug?


the problem is you are assigning a pointer to a temporary string, that substr() creates
m.

> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org 
> [mailto:gcc-help-owner@gcc.gnu.org]On Behalf Of Kate minc
> Sent: Wednesday, November 06, 2002 12:00 PM
> To: Alexandre Courbot
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: g++/stl bug?
> 
> 
> 
> Ok.
> The following code still does not work:
> 
> #include <iostream>
> using namespace std;
> 
> int main() {
>  string one="one";
>  string two="two";
>  char *onepart=(char *)one.substr(1,1).c_str();
>  cout<<onepart<<endl;
>  char *twopart=(char *)two.substr(1,1).c_str();
>  cout<<onepart<<"-"<<twopart<<endl;
>  return 0;
> }
> Output:
> n
> w-w
> 
> Is this a problem of the string class?
> 
> 
> --- Alexandre Courbot
> <alexandrecourbot@linuxgames.com> wrote:
> > >  char *onepart=(char*)one.substr(1,1).data();
> > 
> > Hi,
> > 
> > First you should use c_str() instead of data().
> > data() might well return
> > you a non-null terminated string or a bunch of trash
> > - c_str() is the
> > correct function to use, and you won't need to cast
> > the pointer. Have a
> > read at http://www.sgi.com/tech/stl/ for more
> > informations about the
> > string class. I think this should be enough to solve
> > your problem.
> > 
> > > PS: I am using g++ version 2.96. OS: Redhat 7.2
> > > On using g++3 it is says "string undeclared"!
> > 
> > gcc 3.0 is much more demanding about the
> > standardness of your C++ code.
> > The string class belongs to the std namespace in
> > ansi C++. While gcc
> > 2.96 used std as the "root" namespace (or at least
> > seems to), gcc 3.0
> > doesn't. To make your program compile under gcc 3.0,
> > use "std::string"
> > instead of "string", or add a "using std::string" or
> > "using namespace
> > std" (latter not recommanded) before using any
> > string. Bruce Eckel has a
> > great chapter about namespaces and their use in his
> > Thinking in C++ book
> > - it is available freely on the web at
> >
> http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
> > and is truly a
> > perl, I can only recommand it if you want a great
> > intermediate book
> > about standard C++.
> > 
> > Hope this help!
> > Alex.
> > -- 
> > http://www.gnurou.org
> 
> 
> __________________________________________________
> Do you Yahoo!?
> HotJobs - Search new jobs daily now
> http://hotjobs.yahoo.com/
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]