I have a code fragment in VS8 (VS.2005):

char* fn() {
ostringstram x;
x << "text";
return x.str();
}

It returns and error message:
C2440: 'return' : cannot convert from
'std::basic_std::basic_sgtring<_Elem,_Traits,_Ax>' to 'char *'

The online help says x.str() return 'char *str( );"
Tracing through the examples, 'x.rdbuf()->str()'; has the same result.'

What in tarnation am I missing?

Re: ostringstream x.str() does not return (char*) by Ben

Ben
Wed Dec 05 11:25:46 PST 2007


"skidmarks" <skidmarks@discussions.microsoft.com> wrote in message
news:E2AF4A82-8C84-4E11-A9FB-54D8EC9013E0@microsoft.com...
>I have a code fragment in VS8 (VS.2005):
>
> char* fn() {
> ostringstram x;
> x << "text";
> return x.str();
> }
>
> It returns and error message:
> C2440: 'return' : cannot convert from
> 'std::basic_std::basic_sgtring<_Elem,_Traits,_Ax>' to 'char *'

Looks like it returns a std::string, you can call c_str on that to get a
C-style pointer.

>
> The online help says x.str() return 'char *str( );"


My documentation page for basic_ostringstream::str shows a return type of
basic_string, perhaps the char* is from the old iostreams (before they
became part of the C++ Standard Library).

> Tracing through the examples, 'x.rdbuf()->str()'; has the same result.'
>
> What in tarnation am I missing?
>



Re: ostringstream x.str() does not return (char*) by Igor

Igor
Wed Dec 05 11:25:02 PST 2007

skidmarks <skidmarks@discussions.microsoft.com> wrote:
> I have a code fragment in VS8 (VS.2005):
>
> char* fn() {
> ostringstram x;
> x << "text";
> return x.str();
> }
>
> It returns and error message:
> C2440: 'return' : cannot convert from
> 'std::basic_std::basic_sgtring<_Elem,_Traits,_Ax>' to 'char *'
>
> The online help says x.str() return 'char *str( );"

My copy of the documentation says ostringstream::str returns string, not
char*. Can you give the URL to the article you are citing?

You have a more serious design problem. Realize that 'x' goes out of
scope when the function ends, is destroyed and frees all the buffers it
might have allocated. What memory is the char* pointer you are returning
going to point to?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: ostringstream x.str() does not return (char*) by Tom

Tom
Thu Dec 06 03:49:41 PST 2007

skidmarks wrote:
> I have a code fragment in VS8 (VS.2005):
>
> char* fn() {

std::string fn {
and your problems will be solved (or at least moved elsewhere).
Returning a char* is problematic, as Igor explained.

Tom