stringstream constructor

Started by
4 comments, last by thedustbustr 16 years, 9 months ago
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
Advertisement
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.
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).
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.
This line:
std::stringstream info();
Is getting parsed as a function declairation.

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

This topic is closed to new replies.

Advertisement