2014-09-19 85 views
1

下面的代碼片段可以用字符串編譯,但不能用<wstring>編譯。使用gzip_compressor和boost :: iostreams :: filtering_wostream,編譯器錯誤

我以前filtering_ostream<string>

const void X::Compress(const wstring& data) 
{ 
    wstring compressedString; 
    boost::iostreams::filtering_wostream compressingStream; 
    compressingStream.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(boost::iostreams::gzip::best_compression))); 
    compressingStream.push(boost::iostreams::back_inserter(compressedString)); 
    compressingStream << data; 
    boost::iostreams::close(compressingStream); 

    //... 
} 

編譯錯誤:

\boost_x64/boost/iostreams/chain.hpp:258:9: error: no matching function for call to std::list<boost::iostreams::detail::linked_streambuf<wchar_t, std::char_traits<wchar_t> >*, 
std::allocator<boost::iostreams::detail::linked_streambuf<wchar_t, std::char_traits<wchar_t> >*> >::push_backn(std::auto_ptr<boost::iostreams::stream_buffer<boost::iostreams::basic_gzip_compressor<>, std::char_traits<wchar_t>, std::allocator<wchar_t>, boost::iostreams::output> >::element_type*) 
     list().push_back(buf.get()); 
    ^
+1

的gzip預計'char'流。你或許可以用codecvt的事實來實現某些東西,但坦率地說,我沒有那麼方便。然後我會+1 – sehe 2014-09-19 17:09:07

回答

1

你應該寫字節序列流,像sehe提到

事情是這樣的:

io::filtering_ostream out; 
// ... 
out.write(reinterpret_cast<const char*>(&data[0]), data.length() * sizeof(wchar_t)); 
// ... 

這樣你就不會有任何錯誤。

全碼:

#include <stdlib.h> 
#include <iostream> 
#include <string> 
#include <algorithm> 
#include <boost/iostreams/filtering_stream.hpp> 
#include <boost/iostreams/filter/gzip.hpp> 

using namespace std; 
namespace io = boost::iostreams; 

int main(int argc, char* argv[]) { 
    wstring wdata(L"SAMPLE_TEXT"); 

    // compress 
    vector<char> packed; 
    io::filtering_ostream out; 
    out.push(io::gzip_compressor(io::gzip_params(io::gzip::best_compression))); 
    out.push(io::back_inserter(packed)); 
    out.write(reinterpret_cast<const char*>(&wdata[0]), wdata.length() * sizeof(wchar_t)); 
    io::close(out); 

    // decompress 
    vector<char> unpacked; 
    io::filtering_ostream dec; 
    dec.push(io::gzip_decompressor()); 
    dec.push(io::back_inserter(unpacked)); 
    dec.write(&packed[0], packed.size()); 
    io::close(dec); 

    // print decompressed data 
    wstring result(reinterpret_cast<const wchar_t*>(&unpacked[0]), unpacked.size()/sizeof(wchar_t)); 
    wcout << result << endl; // prints: SAMPLE_TEXT 

    return EXIT_SUCCESS; 
}