[C++] strange problem with std::ostringstream

E

Eric Boutin

Hi ! I have a strange problem with a std::eek:stringstream..

code :
#include <sstream>

/*...*/

std::eek:stringstream ss();
ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\" \"\""
<< outfilename << "\"\""; //line 75

std::string callstring = ss.str(); //line 77

/* ... */

I don't have a undeclared identifier error when declaring std::eek:stringstream
ss();
however, I have thoses stranges errors :

c:\documents and settings\eric\mes documents\programming\stdcpp dynamic
code\loadlib.hpp(75) : error C2296: '<<' : illegal, left operand has type
'class std::basic_ostringstream<char,struct std::char_traits<char>,class
std::allocator<char> > (__cdec
l *)(void)'
c:\documents and settings\eric\mes documents\programming\stdcpp dynamic
code\loadlib.hpp(75) : error C2297: '<<' : illegal, right operand has type
'char [3]'
c:\documents and settings\eric\mes documents\programming\stdcpp dynamic
code\loadlib.hpp(77) : error C2228: left of '.str' must have
class/struct/union type


I use vc++ 6.0

that's a very strange error and I really do not have a clue how to solve
it.. I mean.. ss is not a undeclared identifier.. and it's really defined
as a std::eek:stringstream.. but I have thoses errors...


any help appreciated.. really !!

thanks !

-Eric Boutin
 
R

Rob Williscroft

Eric Boutin wrote in
This:
std::eek:stringstream ss();

is a function declaration

std::eek:stringstream ss( void );

change it to

std::eek:stringstream ss;

Rob.
 
R

Rob Williscroft

Eric Boutin wrote in
This is a normal behavior or a behavior from a crappy compiler ?

Its normal standard conforming behaviour.

[snip]. <<-- hint

Rob.
 
D

Dan W.

Eric Boutin wrote in
This is a normal behavior or a behavior from a crappy compiler ?

Its normal standard conforming behaviour.

[snip]. <<-- hint

Rob.

I've always been a bit confused by declarations of variables
explicitly calling the default constructor. And now I wonder how would
the compiler not interpret

std::eek:stringstream ss();

as a forward declaration of a function returning an ostringstream.
When exactly is it necessary to add "()" to class instantiation?

Would that only be when instantiating a derived type in the argument
list of a function call like

class bar {};
void foo(bar);
class dbar : bar {};
...........
foo( dbar temp_db() );
?

dan
 
R

Rob Williscroft

Dan W. wrote in
I've always been a bit confused by declarations of variables
explicitly calling the default constructor. And now I wonder how would
the compiler not interpret

std::eek:stringstream ss();

as a forward declaration of a function returning an ostringstream.
When exactly is it necessary to add "()" to class instantiation?

Well never (almost). You could write:

std::eek:stringstream ss = std::eek:stringstream();

Unfortunatly it won't work in this case as the streams lack copy-ctor's
but *fortunatly* it unnesassery as the compiler will default construct
ss for you anyway.
Would that only be when instantiating a derived type in the argument
list of a function call like

The one and only time it's truly usefull to explicitly call a default
constructor is when initialising a POD (int, double, struct without
constructors etc).

struct X
{
int a; /* a is POD no default constuctor */
X() : a() {}
};

Though a( 0 ) would be just as good above. This is truly usfull within
templates.

template < typename T > struct Y
{
T b;
Y() : b() {}
};

Now Y::b is properly initialized even if T is a type (say int)
without a default constructor. Also unless for some reason we
know that T will always be initializable from 0, b( 0 ) won't
work.
class bar {};
void foo(bar);
class dbar : bar {};
..........
foo( dbar temp_db() );

Did you mean

foo( bar() );

or maybe

foo( dbar() ); // watch the slicing !!


struct Z { /*whatever*/ };

int foo( Z const &z = Z() )
{
return 0;
}

Not from all of the above the only place I write variable_name() is
in the initialization list of stucts X and Y, otherwise its type().


Rob.
 
D

Dan W.

Thank you!

I guess I had foggy memories coming up, like in nameless temporary
construction, such as

class foggy_memory {};
....
throw( foggy_memory() );

Such off sightings often throw me off for a sec... ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top