Search code examples
c++boost

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


I tried to use boost library with the code below, but it got a bunch of error when I tried to compile it

#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();

}

and I got a bunch of errors , I'm using VS 2013 on a Windows 7 machine(I was trying compatible of Win 7 even WinXP)

any idea how to resolve this problem ? thank you so much

the error output is:

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 ==========

Solution

  • You need to examine the error output in detail to determine what the problem is.

    If we start from the end of the first error message:

    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
    

    This tells you where in your code the error originates from. push only accepts a boost::iostreams device or a std::iostream. You are passing a std::string. If we now look at the start of the error message it starts to make more sense (if you know a little about how boost::iostreams works):

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

    push is trying to work out what to do with your passed argument, it isn't a std::iostream so it assumes it is a boost::iostreams device, all devices export a category member which defines what the devices can be used for. std::string doesn't export such a member so causes an error.

    Assuming base64_decode returns a std::string then you can change compressed to a stream and therefore a valid parameter for push by doing:

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