Hi guys recently i'm starting to understand better c++ and i found different problems and most of them are starting to be clear. One thing that i've not understood is a error that the compiler found when i try to declare a ostream or the every stream in a class declaration. For example
class Test{
stringbuff buff;
ostream out (&buff)
; }
The compiler returns this error message:
expected identifier before ‘&’ token
Oneother is when i try with:
stringstream stream(std::in|std::out);
compiler returns
error: ‘std::ios<char, std::char_traits<char> >::in’ is not a type
stringstream out(ios::in|ios::out);
The question is why can't i call these 'functions' in class declaration and what king of methods are. For example to be more clear how can declare a same methoud to use in this in the same way of ostream o (method);
Thanks to all and sorry for my english.
Your problem is that statement ostream out (&buff) ;
is treated by compiler as an attempt to declare a function member, not a data member ; that is a generalized case of the Most vexing parse.
"Using the new uniform initialization syntax introduced in C++11 solves this issue" for in-class initialization also : ostream out{ &buff };
.
To be more specific, c++11 just allows you to use direct-initialization with {}
or copy-initialization with =
, and not ()
in any of its "direct-init" usages for data members in-class initialization.
Another option is to initialize your data member within constructor's init list.
class Test
{
std::stringbuf buff ;
std::ostream out ;
public :
Test () : out( & buff ) { }
} ;