The accepted answer to this other question lead me to this sample, but compiling it give a long error list. Here the sample code, I added just the includes and a dummy main():
#include <boost/spirit/include/qi.hpp>
#include <vector>
#include <map>
#include <string>
#include <iostream>
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct keys_and_values
: qi::grammar<Iterator, std::map<std::string, std::string>()>
{
keys_and_values()
: keys_and_values::base_type(query)
{
query = pair >> *((qi::lit(';') | '&') >> pair);
pair = key >> -('=' >> value);
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
value = +qi::char_("a-zA-Z_0-9");
}
qi::rule<Iterator, std::map<std::string, std::string>()> query;
qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
qi::rule<Iterator, std::string()> key, value;
};
int main(int argc, char **argv)
{
std::string input("key1=value1;key2;key3=value3"); // input to parse
std::string::iterator begin = input.begin();
std::string::iterator end = input.end();
keys_and_values<std::string::iterator> p; // create instance of parser
std::map<std::string, std::string> m; // map to receive results
bool result = qi::parse(begin, end, p, m); // returns true if successful
}
I've tried both boost 1.42 (default on my Ubuntu 11.04 distro), and 1.48 (downloaded). Errors (I report those filtered by QtCreator) differ: ver 1.42 gives
/usr/include/boost/fusion/support/tag_of.hpp:92:13: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::not_<boost::fusion::detail::is_specialized<std::pair<std::basic_string<char>, std::basic_string<char> > > >::************)’
/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(std::pair<std::basic_string<char>, std::basic_string<char> >&)’
/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(mpl_::void_&)’
while ver. 1.48 gives
/home/carlo/Projects/spirit_vect_literals-build-desktop/../../cpp/boost_1_48_0/boost/spirit/home/qi/detail/assign_to.hpp:123: error: no matching function for call to ‘std::pair<std::basic_string<char>, std::basic_string<char> >::pair(const std::basic_string<char>&)’
Do I have something missing?
edit: I've found the solution: add this header and both versions compile
#include <boost/fusion/adapted/std_pair.hpp>
Congratulations on tracking this down... I hit the same error a few weeks back and wasted hours with it. As you found, the solution is just to include this:
#include <boost/fusion/adapted/std_pair.hpp>
which provides the necessary magic for Qi to use std::pair as the output of a rule.
I'm mainly leaving this answer here just so the question no longer shows up as unanswered - if you want to add/accept your own answer I'll retract this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With