我试着使用boost,但在编译代码时出现了一堆错误



我试图用下面的代码使用boost库,但当我试图编译时,它遇到了一堆错误

#include <string>
#include <sstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
std::string base64_decode(const std::string&);
std::string base64_encode(const unsigned char*, size_t);
using std::string;
void zipstr(string uncompressedstr, string & compressedstr)
{
std::stringstream compressed;
std::stringstream original;
original << uncompressedstr;
boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
out.push(boost::iostreams::zlib_compressor());
out.push(original);
boost::iostreams::copy(out, compressed);
/**need to encode here **/
compressedstr = base64_encode(reinterpret_cast<const unsigned char*>(compressed.str().c_str()), compressed.str().length());

}
void unzipstr(string compressedstr, string &uncompressedstr)
{
std::stringstream compressed_encoded;
std::stringstream decompressed;
compressed_encoded << compressedstr;
/** first decode  then decompress **/
std::string compressed = base64_decode(compressed_encoded.str());
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(compressed);
uncompressedstr=decompressed.str();
}

我遇到了一堆错误,我在Windows7机器上使用VS2013(我试图兼容Win7甚至WinXP(

知道如何解决这个问题吗?非常感谢

错误输出为:

1>------ Rebuild All started: Project: myproject, Configuration: Debug Win32 ------
1>  stdafx.cpp
1>  myproject.cpp
1>d:cpptestnewmyprojectmyproject.cpp(548): warning C4018: '<' : signed/unsigned mismatch
1>d:opensourcelibrarycppboost_1_59_0boostiostreamstraits.hpp(242): error C2039: 'category' : is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>          d:opensourcelibrarycppboost_1_59_0boostmpleval_if.hpp(41) : see reference to class template instantiation 'boost::iostreams::detail::member_category<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>' being compiled
1>          d:opensourcelibrarycppboost_1_59_0boostiostreamstraits.hpp(274) : see reference to class template instantiation 'boost::mpl::eval_if<boost::mpl::and_<boost::iostreams::is_std_io<std::string>,boost::mpl::not_<boost::iostreams::detail::is_boost<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>,boost::mpl::true_,boost::mpl::true_,boost::mpl::true_>,boost::iostreams::select<boost::iostreams::is_filebuf<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::filebuf_tag,boost::iostreams::is_ifstream<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::ifstream_tag,boost::iostreams::is_ofstream<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::ofstream_tag,boost::iostreams::is_fstream<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::fstream_tag,boost::iostreams::is_stringbuf<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::stringbuf_tag,boost::iostreams::is_istringstream<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::istringstream_tag,boost::iostreams::is_ostringstream<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::ostringstream_tag,boost::iostreams::is_stringstream<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>,boost::iostreams::stringstream_tag,boost::iostreams::is_streambuf<T>,boost::iostreams::generic_streambuf_tag,boost::iostreams::is_iostream<T>,boost::iostreams::generic_iostream_tag,boost::iostreams::is_istream<T>,boost::iostreams::generic_istream_tag,boost::iostreams::is_ostream<T>,boost::iostreams::generic_ostream_tag>,boost::iostreams::detail::member_category<T>>' being compiled
1>          with
1>          [
1>              T=std::string
1>          ]
1>              d:opensourcelibrarycppboost_1_59_0boostiostreamschain.hpp(236) : see reference to class template instantiation 'boost::iostreams::category_of<T>' being compiled
1>          with
1>          [
1>              T=std::string
1>          ]
1>          d:opensourcelibrarycppboost_1_59_0boostiostreamschain.hpp(216) : see reference to function template instantiation 'void boost::iostreams::detail::chain_base<boost::iostreams::chain<Mode,Ch,Tr,Alloc>,Ch,Tr,Alloc,Mode>::push_impl<T>(const T &,std::streamsize,std::streamsize)' being compiled
1>          with
1>          [
1>              Mode=boost::iostreams::input
1>  ,            Ch=char
1>  ,            Tr=std::char_traits<char>
1>  ,            Alloc=std::allocator<char>
1>  ,            T=std::string
1>          ]
1>          d:opensourcelibrarycppboost_1_59_0boostiostreamschain.hpp(216) : see reference to function template instantiation 'void boost::iostreams::detail::chain_base<boost::iostreams::chain<Mode,Ch,Tr,Alloc>,Ch,Tr,Alloc,Mode>::push_impl<T>(const T &,std::streamsize,std::streamsize)' being compiled
1>          with
1>          [
1>              Mode=boost::iostreams::input
1>  ,            Ch=char
1>  ,            Tr=std::char_traits<char>
1>  ,            Alloc=std::allocator<char>
1>  ,            T=std::string
1>          ]
1>          d:opensourcelibrarycppboost_1_59_0boostiostreamschain.hpp(499) : see reference to function template instantiation 'void boost::iostreams::detail::chain_base<boost::iostreams::chain<Mode,Ch,Tr,Alloc>,Ch,Tr,Alloc,Mode>::push<T>(const T &,std::streamsize,std::streamsize,void *)' being compiled
1>          with
1>          [
1>              Mode=boost::iostreams::input
1>  ,            Ch=char
1>  ,            Tr=std::char_traits<char>
1>  ,            Alloc=std::allocator<char>
1>  ,            T=std::string
1>          ]
1>          d:opensourcelibrarycppboost_1_59_0boostiostreamschain.hpp(499) : see reference to function template instantiation 'void boost::iostreams::detail::chain_base<boost::iostreams::chain<Mode,Ch,Tr,Alloc>,Ch,Tr,Alloc,Mode>::push<T>(const T &,std::streamsize,std::streamsize,void *)' being compiled
1>          with
1>          [
1>              Mode=boost::iostreams::input
1>  ,            Ch=char
1>  ,            Tr=std::char_traits<char>
1>  ,            Alloc=std::allocator<char>
1>  ,            T=std::string
1>          ]
1>          d:opensourcelibrarycppboost_1_59_0boostiostreamschain.hpp(488) : see reference to function template instantiation 'void boost::iostreams::detail::chain_client<Self>::push_impl<T>(const T &,std::streamsize,std::streamsize)' being compiled
1>          with
1>          [
1>              Self=boost::iostreams::chain<boost::iostreams::input,char,std::char_traits<char>,std::allocator<char>>
1>  ,            T=std::string
1>          ]
1>          d:opensourcelibrarycppboost_1_59_0boostiostreamschain.hpp(488) : see reference to function template instantiation 'void boost::iostreams::detail::chain_client<Self>::push_impl<T>(const T &,std::streamsize,std::streamsize)' being compiled
1>          with
1>          [
1>              Self=boost::iostreams::chain<boost::iostreams::input,char,std::char_traits<char>,std::allocator<char>>
1>  ,            T=std::string
1>          ]
1>          d:cpptestnewmyprojectmyproject.cpp(218) : see reference to function template instantiation 'void boost::iostreams::detail::chain_client<Self>::push<std::string>(const T &,std::streamsize,std::streamsize,void *)' being compiled
1>          with
1>          [
1>              Self=boost::iostreams::chain<boost::iostreams::input,char,std::char_traits<char>,std::allocator<char>>
1>  ,            T=std::string
1>          ]
1>           d:cpptestnewmyprojectmyproject.cpp(218) : see reference to function template instantiation 'void boost::iostreams::detail::chain_client<Self>::push<std::string>(const T &,std::streamsize,std::streamsize,void *)' being compiled
1>          with
1>          [
1>              Self=boost::iostreams::chain<boost::iostreams::input,char,std::char_traits<char>,std::allocator<char>>
1>  ,            T=std::string
1>          ]
1>d:opensourcelibrarycppboost_1_59_0boostiostreamstraits.hpp(242): error C2146: syntax error : missing ';' before identifier 'type'
1>d:opensourcelibrarycppboost_1_59_0boostiostreamstraits.hpp(242): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:opensourcelibrarycppboost_1_59_0boostiostreamstraits.hpp(242): error C2208: 'boost::type' : no members defined using this type
1>d:opensourcelibrarycppboost_1_59_0boostiostreamstraits.hpp(242): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>  base64.cpp
1>  Generating Code...
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

您需要详细检查错误输出以确定问题所在。

如果我们从第一条错误消息的末尾开始:

myproject.cpp(218) : see reference to function template instantiation 'void boost::iostreams::detail::chain_client<Self>::push<std::string>(const T &,std::streamsize,std::streamsize,void *)' being compiled

这告诉您代码中错误的来源。push只接受boost::iostreams设备或std::iostream。您正在通过std::string。如果我们现在看看错误消息的开头,它开始更有意义了(如果你对boost::iostreams的工作原理有一点了解的话(:

'category' : is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'

push正试图找出如何处理您传递的参数,它不是std::iostream,因此它假设它是一个boost::iostreams设备,所有设备都导出一个category成员,该成员定义了设备的用途。std::string不导出这样的成员,因此会导致错误。

假设base64_decode返回一个std::string,那么您可以通过执行以下操作将compressed更改为流,从而为push更改一个有效参数:

std::stringstream compressed{base64_decode(compressed_encoded.str())};

最新更新