Sign in to follow this  
thedustbustr

stringstream constructor

Recommended Posts

this code
std::stringstream info();
info << point.x << point.y << point.z << u << v;
OutputDebugString(info.str().c_str());
causes this error
error C2296: '<<' : illegal, left operand has type 'std::stringstream (__cdecl *)(void)'
error C2297: '<<' : illegal, right operand has type 'const float'
error C2228: left of '.str' must have class/struct/union
error C2228: left of '.c_str' must have class/struct/union
though this code (omit parentheses)
std::stringstream info  ;
info << point.x << point.y << point.z << u << v;
OutputDebugString(info.str().c_str());
yields no error, but im unconvinced that it is correct. shouldn't they have identical functionality anyway? whats up? x,y,z are floats

Share this post


Link to post
Share on other sites
std::stringstream info(); doesn't actually construct a stringstream called info. It declares a function called info that returns a stringstream and takes no parameters. It's equivalent to std::stringstream info(void);

Basically you'll have to live without the parenthesis.

Share this post


Link to post
Share on other sites
Quote:
std::stringstream info();
The compiler parses this as a function prototype and gets confused (this is why we omit the parentheses in this context, as in your second example).

Share this post


Link to post
Share on other sites
std::stringstream info();


This is declaration of a function and it doesn't do what you want it to.


class X
{
X( int y = 17 );
}


X x(y);


or

X x; // same as x(17);



are declarations of a variable.

Share this post


Link to post
Share on other sites
This line:
std::stringstream info();
Is getting parsed as a function declairation.

Edit: Too slow x 3, I must be having an off day...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this