我无法让语法起作用,所以我简化了它,直到它只解析一个整数。仍然无法让它工作。它是以下语法:
template<typename Iterator>
struct rangeGrammar : qi::grammar<Iterator, int()>
{
rangeGrammar() :
rangeGrammar::base_type(number)
{
using qi::int_;
using qi::_1;
using qi::_val;
number = int_[_val = _1];
}
qi::rule<Iterator, int()> number;
};
它应该只解析一个整数(我知道我可以告诉解析函数使用 int_ 作为语法,但我不知道这个例子中有什么问题)。
我的解析函数是:
/* n is a std::string provided by the user */
rangeGrammar<std::string::const_iterator> grammar;
int num = 0;
qi::phrase_parse(n.start(), n.end(), grammar, num);
std::cout << "Number: " << num << std::endl;
我收到以下编译器错误:
/boost/spirit/home/qi/reference.hpp: 在成员函数'bool boost::spirit::qi::reference::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Iterator = __gnu_cxx ::__normal_iterator >, Context = boost::spirit::context, boost::spirit::locals<> >, Skipper = boost::spirit::unused_type, Attribute = int, Subject = const boost::spirit::qi ::rule<__gnu_cxx::__normal_iterator >, int(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>]': /boost/spirit/home/qi/ parse.hpp:89:82: 从 'bool boost::spirit::qi::parse(Iterator&, Iterator, const Expr&, Attr&) [with Iterator = __gnu_cxx::__normal_iterator >, Expr = rangeGrammar<__gnu_cxx::__normal_iterator 实例化>>, Attr = int]' ../parameter_parser.h:95:46:从这里实例化 boost/spirit/home/qi/reference.hpp:43:71: 错误: no matching function for call to 'boost::spirit::qi::rule<__gnu_cxx::__normal_iterator >, int(), boost ::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>::parse(__gnu_cxx::__normal_iterator >&, const __gnu_cxx::__normal_iterator >&, boost::spirit::context, boost::spirit::locals<> >&, const boost::spirit::unused_type&, int&) const' cc1plus: 警告被视为错误 /boost/spirit/home/qi/reference.hpp:44:9: 错误: 控制到达非空函数的结尾used_type>::parse(__gnu_cxx::__normal_iterator >&, const __gnu_cxx::__normal_iterator >&, boost::spirit::context, boost::spirit::locals<> >&, const boost::spirit::unused_type&, int&) const' cc1plus: 警告被视为错误 /boost/spirit/home/qi/reference.hpp:44:9: 错误: 控制到达非空函数的末尾used_type>::parse(__gnu_cxx::__normal_iterator >&, const __gnu_cxx::__normal_iterator >&, boost::spirit::context, boost::spirit::locals<> >&, const boost::spirit::unused_type&, int&) const' cc1plus: 警告被视为错误 /boost/spirit/home/qi/reference.hpp:44:9: 错误: 控制到达非空函数的末尾 * 退出状态 1 *
无法弄清楚问题是什么。任何帮助将不胜感激。