我在VS 2010下的boost中遇到了zlib库的问题。我构建了库,并在boost / stage / lib文件夹中生成了相应的dll / libs。我将.dll添加到我的程序调试文件夹中,并在matching.lib中链接。
但是当我真正尝试使用zlib流时,我遇到了问题。下面是一个例子:
#include <cstring>
#include <string>
#include <iostream>
#include <boost\iostreams\filter\gzip.hpp>
#include <boost\iostreams\filtering_streambuf.hpp>
#include <boost\iostreams\copy.hpp>
std::string DecompressString(const std::string &compressedString)
{
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(compressedString);
std::string retString = "";
copy(in, retString);
return retString;
}
when I try to compile thise though, I get multiple errors including:
error C2039: 'char_type' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' c:\program files (x86)\boost\boost_1_46_0\boost\iostreams\traits.hpp
error C2208: 'boost::type' : no members defined using this type c:\program files (x86)\boost\boost_1_46_0\boost\iostreams\traits.hpp
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\boost\boost_1_46_0\boost\iostreams\traits.hpp
如果我将代码更改为以下内容:
std::string DecompressString(const std::string &compressedString)
{
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
std::string retString = "";
return retString;
}
它编译,意味着问题在于compress.smp的in.push和retString的复制。难道我做错了什么?我不允许使用这样的字符串吗?
提前致谢
答案 0 :(得分:1)
试试这个:
#include <string>
#include <iostream>
#include <sstream>
#include <boost\iostreams\filter\zlib.hpp>
#include <boost\iostreams\filtering_streambuf.hpp>
#include <boost\iostreams\copy.hpp>
std::string DecompressString(const std::string &compressedString)
{
std::stringstream src(compressedString);
boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
in.push(boost::iostreams::zlib_decompressor());
boost::iostreams::copy(in, out);
return dst.str();
}
基本问题似乎是你试图在字符串类型而不是流类型上使用boost::iostreams::copy()
。此外,包括zlib.hpp
而不是gzip.hpp
可能也不会造成伤害。