c++ - 用 boost 写一个 gz 流

标签 c++ boost gzip

我编写了一段代码,使用 boost 将输出写入 gz 文件。

class foo() {
  std::ofstream traceOut;
  struct traceRec {
    traceRec(uint64_t c) : cycle(c) {};
    uint64_t cycle;
  };
  void writeTrace(traceRec &rec)
  {
    try {
      boost::iostreams::filtering_istream in;
      in.push(boost::iostreams::gzip_decompressor());
      in.push(traceOut);   // ERROR
      std::cout << rec.cycle << std::endl;
    }
    catch(const boost::iostreams::gzip_error& e) {
      std::cout << e.what() << '\n';
    }
  }
  void init() {
    traceOut.open("log.gz", std::ios_base::in | std::ios_base::binary);
  }
  void bar() {
    traceRec rec (1000);
    writeTrace(rec);
  }
};

编译时出现这个错误

/opt/boost_1_33_1/boost/iostreams/chain.hpp: In member function ‘void boost::iostreams::detail::chain_client<Chain>::push(std::basic_ostream<_CharT2,      _Traits2>&, int, int) [with CharType = char, TraitsType = std::char_traits<char>, Chain = boost::iostreams::chain<boost::iostreams::input, char, 
std::char_traits<char>, std::allocator<char> >]’:
test.cpp:   instantiated from here
/opt/boost_1_33_1/boost/iostreams/chain.hpp:472: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’ 
/opt/boost_1_33_1/boost/iostreams/traits.hpp: At global scope:
/opt/boost_1_33_1/boost/iostreams/traits.hpp: In instantiation of ‘boost::iostreams::detail::member_category<mpl_::void_>’:
/opt/boost_1_33_1/boost/mpl/eval_if.hpp:38:   instantiated from ‘boost::mpl::eval_if<boost::iostreams::is_std_io<mpl_::void_>, boost::iostreams::select<boost::iostreams::is_iostream<mpl_::void_>, boost::iostreams::iostream_tag, boost::iostreams::is_istream<mpl_::void_>, boost::iostreams::istream_tag, boost::iostreams::is_ostream<mpl_::void_>, boost::iostreams::ostream_tag, boost::iostreams::is_streambuf<mpl_::void_>, boost::iostreams::streambuf_tag, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_>, boost::iostreams::detail::member_category<mpl_::void_> >’
....

这是什么意思?

最佳答案

请看this , push() 方法必须接收流而不是任意对象

EDIT1(以下评论交流)

traceOut 的类型更改为 std::ifstream 解决了构建问题(至少使用 boost 1.52)

关于c++ - 用 boost 写一个 gz 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15394609/

相关文章:

c++ - 我怎么知道应该使用哪个 C++ 标准版本来构建哪个版本的 Boost?

http - 为什么 Safari 不接受 GZIP 压缩?

c++ - zlib - gzip 的零压缩率

C++11:确保一次写入多次读取的定义语义?

c++ - CMake 文件 glob 是邪恶的?

c++ - 如何编译静态链接除一个库之外的所有库 (G++)

bash - 从多个目录创建多个 tar.gz 存档

c++ - CUDA 直方图遇到非法内存访问 (77)

c++ - Boost.Graph - 有 2 个简单类和一个类 - 数据类型如何创建图形?

c++ - boost::shared_ptr 错误