6

こんにちは、Boost.IOstreamsを使用してデータをbzip2ファイルに保存したいと思います。

void test_bzip()
{
namespace BI = boost::iostreams;
{
string fname="test.bz2";
  {
    BI::filtering_stream<BI::bidirectional> my_filter; 
    my_filter.push(BI::combine(BI::bzip2_decompressor(), BI::bzip2_compressor())) ; 
    my_filter.push(std::fstream(fname.c_str(), std::ios::binary|std::ios::out)) ; 
    my_filter << "test" ; 

    }//when my_filter is destroyed it is trowing an assertion.
}
};

私が間違っていることは何ですか?Boost1.42.0を使用しています。

アーマンよろしくお願いします。

編集 双方向オプションを削除すると、コードは機能します。

#include <fstream> 
#include <iostream> 
#include <boost/iostreams/copy.hpp> 
#include <boost/iostreams/filter/bzip2.hpp> 
#include <boost/iostreams/device/file.hpp> 
#include <boost/iostreams/filtering_stream.hpp>
#include <string>



void test_bzip()
{
        namespace BI = boost::iostreams;
        {
                std::string fname="test.bz2";
                {
                        std::fstream myfile(fname.c_str(), std::ios::binary|std::ios::out); 
                        BI::filtering_stream<BI::output> my_filter; 
                        my_filter.push(BI::bzip2_compressor()) ; 
                        //my_filter.push(std::fstream(fname.c_str(), std::ios::binary|std::ios::out)) ; //this line will work on VC++ 2008 V9 but not in G++ 4.4.4
                        my_filter.push(myfile);
                        my_filter << "test";
                }
        }
};

多分誰かが理由を説明できますか?

4

1 に答える 1

3

Anfstreamはコピーできないため、Push の参照バージョンを使用する必要があります

template<typename StreamOrStreambuf>
void push( StreamOrStreambuf& t,
           std::streamsize buffer_size = default value,
           std::streamsize pback_size = default value );

したがって、関数は次のようになります

std::fstream theFile(fname.c_str(), std::ios::binary | std::ios::out);
// [...]
my_filter.push(theFile) ; 

コンパイラがあなたのコードを許可していることに驚いています.一時への参照について不平を言っていると思います...どのコンパイラを使用していますか?

于 2010-04-01T09:17:55.547 に答える