Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-12-23 17:13:10


Daniel Schlyder wrote:
> Hi,

Hi,

> I get the following error when trying to compile a simple test
> (attached)
> using vector_sink from the tutorial with MinGW GCC 3.4.2:

I'm sorry it took me so long to look at your test program. There are three
problems:

The first is that streambufs do not support operator<<. You have to use streams
for that. So the first rewrite of your example would look like:

  #include <boost/iostreams/concepts.hpp>
  #include <boost/iostreams/stream_facade.hpp>

  ...

  int main()
  {
    std::vector<char> vec;
    boost::io::stream_facade<vector_sink> out(vector_sink(vec));
    out << 123 << "foo";
    std::copy(
        vec.begin(), vec.end(), std::ostream_iterator<char>(std::cout, "\n"));
  }

The second problem is that the second line is being interpretted as a function
declaration. One way to fix this is:

  int main()
  {
    std::vector<char> vec;
    vector_sink vs(vec);
    boost::io::stream_facade<vector_sink> out(vs);
    out << 123 << "foo";
    std::copy(
        vec.begin(), vec.end(), std::ostream_iterator<char>(std::cout, "\n"));
  }

In the version I am about to release, you can also do this, which is more
elegant:

  #include <boost/ref.hpp>

  int main()
  {
    std::vector<char> vec;
    boost::io::stream_facade<vector_sink> out(boost::ref(vec));
    out << 123 << "foo";
    std::copy(
        vec.begin(), vec.end(), std::ostream_iterator<char>(std::cout, "\n"));
  }

The last problem is that you need to flush or close the stream before you can
rely on the data having being written to the vector.

I hope I've answered your question.

>Daniel Schlyder
> http://bitblaze.com/

Best Regards,
Jonathan


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk