Boost Gzip过滤器:编译失败

时间:2011-10-10 09:46:15

标签: c++ gzip zlib

我正在尝试从Boost Gzip过滤器页面编译示例:

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>

int main() 
{
    using namespace std;

    ifstream file("hello.gz", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(gzip_decompressor());
    in.push(file);
    boost::iostreams::copy(in, cout);
}

可悲的是,我的g ++会返回错误:

gzlib.cpp: In function ‘int main()’:
gzlib.cpp:12:3: error: ‘filtering_streambuf’ was not declared in this scope
gzlib.cpp:12:23: error: ‘input’ was not declared in this scope
gzlib.cpp:12:30: error: ‘in’ was not declared in this scope
gzlib.cpp:13:29: error: ‘gzip_decompressor’ was not declared in this scope

此功能有什么问题以及如何修改它以使其正常工作?非常感谢!

链接到Boost Gzip过滤器:http://www.boost.org/doc/libs/release/libs/iostreams/doc/classes/gzip.html

1 个答案:

答案 0 :(得分:8)

问题是您没有指定要在其中查找filtering_streambufinputgzip_decompressor的命名空间。 尝试:

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>

int main() 
{
    using namespace std;
    using namespace boost::iostreams;
    ifstream file("hello.gz", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(gzip_decompressor());
    in.push(file);
    copy(in, cout);
}

example不执行此操作的原因是因为introduction中建立的约定:

  

除非另有说明,否则文档中介绍的所有类,函数和模板都在命名空间boost :: iostreams中。通常省略命名空间限定。