1

The following code fails to compile:

#include <cstdio> 
#include <sstream>
int main()
{
   std::ostrstream strm;                         
   strm.rdbuf()->freeze(0);                      
}

I get the following errors on compilation:

g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:5: error: 'ostrstream' is not a member of 'std'
sample3.cpp:5: error: expected `;' before 'strm'
sample3.cpp:6: error: 'strm' was not declared in this scope

After searching in google, I suspect that I should use ostringstream in place of ostrstream, so I have modified the program as below:

#include <cstdio> 
#include <sstream>
int main()
{
   std::ostringstream strm;                         
   strm.rdbuf()->freeze(0);                      
}

But now I get the following errors:

g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:6: error: 'struct std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >' has no member named 'freeze'
Axalo
  • 2,953
  • 4
  • 25
  • 39
pankaj kushwaha
  • 369
  • 5
  • 20
  • [freeze()](http://en.cppreference.com/w/cpp/io/strstreambuf/freeze) takes a bool or nothing, but not an int. – Kevin Apr 28 '15 at 11:09
  • 3
    [`std::basic_stringbuf`](http://en.cppreference.com/w/cpp/io/basic_stringbuf) doesn't have a member function named `freeze`, why do you think it should have? The [`strsteambuf`](http://en.cppreference.com/w/cpp/io/strstreambuf) class is *deprecated*, and was from before C++ became standardized in 1998. – Some programmer dude Apr 28 '15 at 11:11
  • @Kevin I am passing 0 to freeze which should implicit type cast to bool ( " FALSE actually ). – pankaj kushwaha Apr 28 '15 at 11:19
  • @JoachimPileborg [rdbuf()](http://en.cppreference.com/w/cpp/io/ostrstream/rdbuf) doesn't return a basic_stringbuf, but a strstreambuf*, which does have a method freeze() – Kevin Apr 28 '15 at 11:21
  • @JoachimPileborg So what i should do in order to use freeze() function , when i declare std::ostrstream , then compiler gives error "'ostrstream' is not a member of 'std'" – pankaj kushwaha Apr 28 '15 at 11:22
  • You're thinking about the pre-standard `ostrstream` class, not the post-standard `ostringstream` which is a completely different thing. And the only thing you can do is to not use `freeze` since it's deprecated and can be removed anytime in the future. You better learn *modern* C++, even if it's the "old" C++03 variant. – Some programmer dude Apr 28 '15 at 11:22
  • @JoachimPileborg : is there no equivalent to freeze function in modern c++? – pankaj kushwaha Apr 28 '15 at 12:06
  • 3
    There's no *need* for a call to the `freeze` member function if you start using an `ostringstream`. Unlike `strstreambuf`, you cannot ask a `basic_stringbuf` to stop managing itself. Figure out what exactly your original code was doing and then try to implement the same functionality using `stringstream` instead of trying to translate line by line, – Praetorian Apr 28 '15 at 13:08
  • @Praetorian : Thanks , I will try to understand my original code. – pankaj kushwaha Apr 28 '15 at 13:29

2 Answers2

3

Just scrap the freeze() call -- the current generation std::ostringstream doesn't expose its memory management guts to you like the old ostrstream did. You'll need to rework your original code to let the stringstream manage memory in the way it wants (it'll be much simpler/less error-prone that way!).

LThode
  • 1,843
  • 1
  • 17
  • 28
0

I should have changed #include "sstream" to #include "strstream" then it will not report the error "'ostrstream' is not a member of 'std'".

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
pankaj kushwaha
  • 369
  • 5
  • 20