This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: g++/stl bug?
- From: Kate minc <kate_minc_2000 at yahoo dot com>
- To: Alexandre Courbot <alexandrecourbot at linuxgames dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 6 Nov 2002 03:00:08 -0800 (PST)
- 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/