* RE: Error with basic string
[not found] <616BE6A276E3714788D2AC35C40CD18D9EDBA4@whale.softwire.co.uk>
@ 2003-01-23 18:32 ` Rupert Wood
2003-01-24 12:33 ` Sebastian Huber
0 siblings, 1 reply; 5+ messages in thread
From: Rupert Wood @ 2003-01-23 18:32 UTC (permalink / raw)
To: 'Ajay Bansal'; +Cc: gcc-help
Ajay Bansal wrote:
> char *func(string a, int n)
> {
> a.resize(n);
> return a.begin();
> }
a.begin() is an iterator. You'll have to dereference it to get the
character and then re-reference that for the pointer, i.e. (bracketed
for clarity)
char *func(string a, int n)
{
a.resize(n);
return &(*(a.begin()));
}
*However*
1) you're not passing your string by reference; what you're
telling it to do is to take a copy of string a, resize the
copy to n characters and then return a pointer to the first
character in the copy - i.e. a pointer to memory owned by a
dead object
2) (I'm not an STL guru so take this with a pinch of salt)
I don't think there's a requirement that the string be
stored continuously in memory, except for a read-only copy
between c_str() the next non-const operation. It looks like
you want func to allocate you an n-byte continuous character
buffer in a basic_string and, whilst some implementations will
give you this, I don't think you can assume all will. (But you
probably know this - the comment about changing the return
type?) I can't tell you if G++'s STL plays along.
Rup.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Error with basic string
2003-01-23 18:32 ` Error with basic string Rupert Wood
@ 2003-01-24 12:33 ` Sebastian Huber
0 siblings, 0 replies; 5+ messages in thread
From: Sebastian Huber @ 2003-01-24 12:33 UTC (permalink / raw)
To: gcc-help
Hello!
On Thursday 23 January 2003 19:31, Rupert Wood wrote:
> Ajay Bansal wrote:
> > char *func(string a, int n)
> > {
> > a.resize(n);
> > return a.begin();
> > }
>
> a.begin() is an iterator. You'll have to dereference it to get the
> character and then re-reference that for the pointer, i.e. (bracketed
> for clarity)
>
> char *func(string a, int n)
> {
> a.resize(n);
> return &(*(a.begin()));
> }
You should use 'return const_cast<char*>( a.data())' or 'a.c_str()'. But you
have to keep in mind, that every non-constant method of std::string may make
the data invalid.
> *However*
>
> 1) you're not passing your string by reference; what you're
> telling it to do is to take a copy of string a, resize the
> copy to n characters and then return a pointer to the first
> character in the copy - i.e. a pointer to memory owned by a
> dead object
>
> 2) (I'm not an STL guru so take this with a pinch of salt)
> I don't think there's a requirement that the string be
> stored continuously in memory, except for a read-only copy
> between c_str() the next non-const operation. It looks like
> you want func to allocate you an n-byte continuous character
> buffer in a basic_string and, whilst some implementations will
> give you this, I don't think you can assume all will. (But you
> probably know this - the comment about changing the return
> type?) I can't tell you if G++'s STL plays along.
>
> Rup.
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: Error with basic string
@ 2003-01-23 18:57 Ajay Bansal
0 siblings, 0 replies; 5+ messages in thread
From: Ajay Bansal @ 2003-01-23 18:57 UTC (permalink / raw)
To: Rupert Wood; +Cc: gcc-help
This is actually not dead object. Func is a member of a class & "string
a" is also a member of the same class.. :)
This was just a sample program..
But anyway. Thanks a lot.. :).. It has worked..
-----Original Message-----
From: Rupert Wood [mailto:me@rupey.net]
Sent: Friday, January 24, 2003 12:02 AM
To: Ajay Bansal
Cc: gcc-help@gcc.gnu.org
Subject: RE: Error with basic string
Ajay Bansal wrote:
> char *func(string a, int n)
> {
> a.resize(n);
> return a.begin();
> }
a.begin() is an iterator. You'll have to dereference it to get the
character and then re-reference that for the pointer, i.e. (bracketed
for clarity)
char *func(string a, int n)
{
a.resize(n);
return &(*(a.begin()));
}
*However*
1) you're not passing your string by reference; what you're
telling it to do is to take a copy of string a, resize the
copy to n characters and then return a pointer to the first
character in the copy - i.e. a pointer to memory owned by a
dead object
2) (I'm not an STL guru so take this with a pinch of salt)
I don't think there's a requirement that the string be
stored continuously in memory, except for a read-only copy
between c_str() the next non-const operation. It looks like
you want func to allocate you an n-byte continuous character
buffer in a basic_string and, whilst some implementations will
give you this, I don't think you can assume all will. (But you
probably know this - the comment about changing the return
type?) I can't tell you if G++'s STL plays along.
Rup.
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: Error with basic string
@ 2003-01-23 18:20 Ajay Bansal
0 siblings, 0 replies; 5+ messages in thread
From: Ajay Bansal @ 2003-01-23 18:20 UTC (permalink / raw)
To: gcc-help
Ooops.. Copy-pasted wrong func defination
Correct function is..........
char *func(string a, int n)
{
a.resize(n);
return a.begin();
}
And error is
-----------------------
string_test.cpp: In function `char* func(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int)':
string_test.cpp:7: cannot convert `__gnu_cxx::__normal_iterator<char*,
std::basic_string<char, std::char_traits<char>, std::allocator<char>
> >' to
`char*' in return
-----Original Message-----
From: Ajay Bansal
Sent: Thursday, January 23, 2003 11:45 PM
To: gcc-help@gcc.gnu.org
Subject: Error with basic string
Hi All
I am trying to compile the following simple function on RH73 with gcc
3.2.1
*********string_test.cpp**************
#include <iostream>
#include <string>
using namespace std;
char * func(string a, int n)
{
a.resize(n);
return (char *)a.begin();
}
int main()
{
string a;
char *b;
b=func(a,5);
return 0;
}
***************
I get the folowing error
[ajay@linux1 stl]$ g++ string_test.cpp
string_test.cpp: In function `char* func(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int)':
string_test.cpp:7: cannot convert `std::basic_string<_CharT, _Traits,
_Alloc>::begin() [with _CharT = char, _Traits =
std::char_traits<char>,
_Alloc = std::allocator<char>]()' from type `
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >' to type `char*'
How to resolve this error. This program works fine on my solaris and HP
machine with their compilers (workshop on sun and aCC on HP)
I can not change the return type of func.
Regards
Ajay
^ permalink raw reply [flat|nested] 5+ messages in thread
* Error with basic string
@ 2003-01-23 18:10 Ajay Bansal
0 siblings, 0 replies; 5+ messages in thread
From: Ajay Bansal @ 2003-01-23 18:10 UTC (permalink / raw)
To: gcc-help
Hi All
I am trying to compile the following simple function on RH73 with gcc
3.2.1
*********string_test.cpp**************
#include <iostream>
#include <string>
using namespace std;
char * func(string a, int n)
{
a.resize(n);
return (char *)a.begin();
}
int main()
{
string a;
char *b;
b=func(a,5);
return 0;
}
***************
I get the folowing error
[ajay@linux1 stl]$ g++ string_test.cpp
string_test.cpp: In function `char* func(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int)':
string_test.cpp:7: cannot convert `std::basic_string<_CharT, _Traits,
_Alloc>::begin() [with _CharT = char, _Traits =
std::char_traits<char>,
_Alloc = std::allocator<char>]()' from type `
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >' to type `char*'
How to resolve this error. This program works fine on my solaris and HP
machine with their compilers (workshop on sun and aCC on HP)
I can not change the return type of func.
Regards
Ajay
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2003-01-24 12:33 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <616BE6A276E3714788D2AC35C40CD18D9EDBA4@whale.softwire.co.uk>
2003-01-23 18:32 ` Error with basic string Rupert Wood
2003-01-24 12:33 ` Sebastian Huber
2003-01-23 18:57 Ajay Bansal
-- strict thread matches above, loose matches on Subject: below --
2003-01-23 18:20 Ajay Bansal
2003-01-23 18:10 Ajay Bansal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).