Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::spirit example doesn't compile

I was following the tutorials in the Spirit documentation, and I'm stuck here:

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>

template <typename Iterator>
bool parse_numbers(Iterator first, Iterator last, double &sum)
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace ph = boost::phoenix;

bool r = qi::phrase_parse(
    first,
    last,
    // begin grammar
    (
        qi::double_[ph::ref(sum) = qi::_1] >> *(',' >> qi::double_[ph::ref(sum) += qi::_1])
    )
    // end grammar
    , ascii::space
);

if (first != last)
    return false;
return true;
}

int main(int argc, char **argv)
{
std::string str = argv[1];
double sum;
bool result = parse_numbers(str.begin(), str.end(), sum);
if (result) {
    std::cout << "Sum: " << sum << std::endl;
    return 0;
}
std::cout << "Invalid Input" << std::endl;
return 1;
}

When I compile it, it shows this error:

$ c++ spirit-test.c++ 
spirit-test.c++: In function ‘bool parse_numbers(Iterator, Iterator, double&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]’:
spirit-test.c++:32:57:   instantiated from here
spirit-test.c++:21:2: error: no match for ‘operator+=’ in ‘boost::phoenix::ref [with T = double](((double&)((double*)sum))) += boost::spirit::_1’
spirit-test.c++:21:2: error: no match for ‘operator=’ in ‘boost::phoenix::ref [with T = double](((double&)((double*)sum))) = boost::spirit::_1’
/usr/include/boost/spirit/home/phoenix/core/actor.hpp:143:16: note: candidate is: boost::phoenix::actor<Eval>& boost::phoenix::actor<Eval>::operator=(const boost::phoenix::actor<Eval>&) [with Eval = boost::phoenix::reference<double>, boost::phoenix::actor<Eval> = boost::phoenix::actor<boost::phoenix::reference<double> >]

What's wrong?

like image 625
mtk358 Avatar asked Mar 26 '11 20:03

mtk358


1 Answers

Add this:

#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

to your includes and it should work.

like image 168
phooji Avatar answered Sep 23 '22 07:09

phooji