7

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?

3
  • Fixed formatting. Please use " . . . " (4 leading spaces) instead of <code>. Commented Mar 26, 2011 at 20:47
  • I don't know of any easy way of inserting spaces before every line of a block of text. Commented Mar 26, 2011 at 20:55
  • Most editors should let you do this -- in emacs, I use M-x increase-indent. With <code>, all phrases inside < and > disappear, so your code looked a lot more wrong (without any template parameters) than it actually was :) Commented Mar 26, 2011 at 20:58

1 Answer 1

9

Add this:

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

to your includes and it should work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.