title: Cannot set the streambuf of an ostringstream object
body: <p>I want to include some <code>std::ostringstream</code> objects in my program for logging and error reporting purposes. Based on a setting given at compile time, the log and error streams will either collect their respective data (to be saved or whatever) or they will be redirected to <code>std::cout</code> and <code>std::cerr</code> (for debugging purposes). As far as I can tell from the <a href="http://www.cplusplus.com/reference/sstream/basic_ostringstream/" rel="nofollow">references</a> (and <a href="http://en.cppreference.com/w/cpp/io/basic_stringstream" rel="nofollow">here</a>) I have looked at, <code>std::ostringstream</code> objects inherit the <code>rdbuf()</code> method and I should be able to call the <code>rdbuf()</code> method on the stream objects, passing as an argument a pointer to the <code>std::streambuf</code> to associate with the streams.  </p>
  
  <p>However, when I try to compile I receive an error for no matching function call, with the <code>rdbuf()</code> method without any arguments listed as a candidate.  </p>
  
  <p>The error I get from g++ (4.6.3 on Ubuntu 12.04 x86_64):  </p>
  
  <pre>
  sigma@SigmaSys:~/Scripts/http/test$ g++ test.cc -c -o test.o
  test.cc: In constructor ‘Logger::Logger()’:
  test.cc:23:43: error: no matching function for call to ‘std::basic_ostringstream::rdbuf(std::basic_streambuf*)’
  test.cc:23:43: note: candidate is:
  /usr/include/c++/4.6/sstream:449:7: note: std::basic_ostringstream::__stringbuf_type* std::basic_ostringstream::rdbuf() const [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator, std::basic_ostringstream::__stringbuf_type = std::basic_stringbuf]
  /usr/include/c++/4.6/sstream:449:7: note:   candidate expects 0 arguments, 1 provided
  </pre>
  
  <p>Here is a stripped down version of the source and header file:  </p>
  
  <p><strong>http_utils.h</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;streambuf&gt;
  
  #define HTTP_DEBUG_MODE 1
  
  
  class Logger {
      private:
          static std::ostringstream errOut;
          static std::ostringstream logOut;
  
          static std::streambuf* cerrStreamBuffBackup;
          static std::streambuf* coutStreamBuffBackup;
  
          static std::streambuf* errOutStreamBuffBackup;
          static std::streambuf* logOutStreamBuffBackup;
      public:
          Logger();
          ~Logger();
  };
  </code></pre>
  
  <p><strong>http_utils.cc</strong>  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "test.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;streambuf&gt;
  
  
  std::ostringstream Logger::errOut;
  std::ostringstream Logger::logOut;
  std::streambuf* Logger::cerrStreamBuffBackup = NULL;
  std::streambuf* Logger::coutStreamBuffBackup = NULL;
  std::streambuf* Logger::errOutStreamBuffBackup = NULL;
  std::streambuf* Logger::logOutStreamBuffBackup = NULL;
  
  
  Logger::Logger() {
      if (HTTP_DEBUG_MODE) {//Redirect logging/error to stdout/stderr
          Logger::errOutStreamBuffBackup = Logger::errOut.rdbuf();
          Logger::logOutStreamBuffBackup = Logger::logOut.rdbuf();
  
          Logger::errOut.rdbuf( std::cerr.rdbuf() );
          Logger::logOut.rdbuf( std::cout.rdbuf() );
      } else {//Redirect stdout/stderr to custom logging/error functionality
          cerrStreamBuffBackup = std::cerr.rdbuf();
          coutStreamBuffBackup = std::cout.rdbuf();
  
          std::cerr.rdbuf( errOut.rdbuf() );
          std::cout.rdbuf( logOut.rdbuf() );
      }
  }
  Logger::~Logger() {
      if (HTTP_DEBUG_MODE) {
  
      } else {//Restore stdout/stderr streambuf
          std::cerr.rdbuf( Logger::cerrStreamBuffBackup );
          std::cout.rdbuf( Logger::coutStreamBuffBackup );
      }
  }
  </code></pre>
  
  <p>If anyone could please help me resolve this, or suggest an alternative method given what I am trying to do, I would be grateful.</p>