By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi ! I have a strange problem with a std::ostringstream..

code :
#include <sstream>

/*...*/

std::ostringstream 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::ostringstream
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::ostringstream.. but I have thoses errors...
any help appreciated.. really !!

thanks !

-Eric Boutin
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Eric Boutin wrote in news:t_**********************@weber.videotron.net:

This:
std::ostringstream ss();


is a function declaration

std::ostringstream ss( void );

change it to

std::ostringstream ss;

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
This is a normal behavior or a behavior from a crappy compiler ?

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> a écrit dans le message de
news:Xn**********************************@195.129. 110.130...
Eric Boutin wrote in news:t_**********************@weber.videotron.net:

This:
std::ostringstream ss();


is a function declaration

std::ostringstream ss( void );

change it to

std::ostringstream ss;

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 22 '05 #3

P: n/a
Eric Boutin wrote in news:eW********************@wagner.videotron.net:
This is a normal behavior or a behavior from a crappy compiler ?


Its normal standard conforming behaviour.

[snip]. <<-- hint

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

P: n/a
On 03 Dec 2003 11:54:53 GMT, Rob Williscroft
<rt*@freenet.REMOVE.co.uk> wrote:
Eric Boutin wrote in news:eW********************@wagner.videotron.net:
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::ostringstream 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
Jul 22 '05 #5

P: n/a
Dan W. wrote in news:8j********************************@4ax.com:
On 03 Dec 2003 11:54:53 GMT, Rob Williscroft
<rt*@freenet.REMOVE.co.uk> wrote:
Eric Boutin wrote in news:eW********************@wagner.videotron.net:
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::ostringstream 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::ostringstream ss = std::ostringstream();

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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

P: n/a
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... ;-)
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.