将gzip_compressor与boost :: iostreams :: filtering_wostream一起使用,编译器错误 [英] using gzip_compressor with boost::iostreams::filtering_wostream, compiler error

查看:81
本文介绍了将gzip_compressor与boost :: iostreams :: filtering_wostream一起使用,编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码段可以使用字符串正确编译,而不能使用<wstring>编译.

Code snippet below compiles fine with string but not with <wstring>.

我将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());
     ^

推荐答案

您应该向流中写入字节序列,就像提到的sehe

You should write a sequence of bytes to the stream, like sehe mentioned

类似这样的东西:

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

这样,您就不会有任何错误.

In this way you won't have any errors.

完整代码:

#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;
}

这篇关于将gzip_compressor与boost :: iostreams :: filtering_wostream一起使用,编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆