我尝试使用 boost 但是当我编译代码时出现了一堆错误

I tried to use boost however it got a bunch of errors when I compile the code

我尝试在下面的代码中使用 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();

}

我遇到了一堆错误,我在 Windows 7 机器上使用 VS 2013(我正在尝试兼容 Win 7 甚至 WinXP)

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

错误输出为:

1>------ Rebuild All started: Project: myproject, Configuration: Debug Win32 ------
1>  stdafx.cpp
1>  myproject.cpp
1>d:\cpptestnew\myproject\myproject.cpp(548): warning C4018: '<' : signed/unsigned mismatch
1>d:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\traits.hpp(242): error C2039: 'category' : is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>          d:\opensourcelibrary\cpp\boost_1_59_0\boost\mpl\eval_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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\traits.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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\chain.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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\chain.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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\chain.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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\chain.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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\chain.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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\chain.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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\chain.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:\cpptestnew\myproject\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
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:\cpptestnew\myproject\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
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:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\traits.hpp(242): error C2146: syntax error : missing ';' before identifier 'type'
1>d:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\traits.hpp(242): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\traits.hpp(242): error C2208: 'boost::type' : no members defined using this type
1>d:\opensourcelibrary\cpp\boost_1_59_0\boost\iostreams\traits.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 returns a std::string 那么你可以将 compressed 更改为流,因此可以通过执行以下操作将 push 的有效参数更改为:

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