boost :: spirit示例无法编译

时间:2011-03-26 20:44:29

标签: c++ parsing boost boost-spirit

我正在遵循Spirit文档中的教程,我被困在这里:

#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;
}

编译时,显示此错误:

$ 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> >]

怎么了?

1 个答案:

答案 0 :(得分:8)

添加:

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

到您的包含,它应该工作。