问题
I'm having a problem with the zlib libraries in boost under VS 2010. I built the libraries and the appropriate dlls/libs were generated in the boost/stage/lib folder. I added the .dlls into my programs debug folder and linked in the matching.lib.
But I'm running into problems when I actually try to use the zlib streams. Heres an example:
#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
If I change my code to the following:
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;
}
It compiles, meaning the problem is with the in.push for compressedString and the copy to the retString. Am I doing something wrong? Am I not allowed to use strings like this?
Thanks in advance
回答1:
Try this:
#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();
}
The basic problem seems to be that you're trying to use boost::iostreams::copy()
on string types rather than stream types. Also, including zlib.hpp
instead of gzip.hpp
probably doesn't hurt either.
来源:https://stackoverflow.com/questions/5380720/boost-zlib-problem