C++ 对Boost::Spirit语法使用迭代器解析

C++ 对Boost::Spirit语法使用迭代器解析,c++,boost,boost-spirit,C++,Boost,Boost Spirit,当我尝试对Spirit语法使用迭代器形式的解析时,我得到一个参数,该参数将转换错误从迭代器类型传递到const char*。我该如何解决这个问题 有一些限制。我在大型输入上使用迭代器适配器,因此转换为C样式的字符串是不可行的 下面是演示该问题的示例代码: #include <boost/spirit/core.hpp> #include <boost/spirit/iterator/file_iterator.hpp> #include <vector> #i

当我尝试对Spirit语法使用迭代器形式的解析时,我得到一个参数,该参数将转换错误从迭代器类型传递到const char*。我该如何解决这个问题

有一些限制。我在大型输入上使用迭代器适配器,因此转换为C样式的字符串是不可行的

下面是演示该问题的示例代码:

#include <boost/spirit/core.hpp>
#include <boost/spirit/iterator/file_iterator.hpp>
#include <vector>
#include <string>
using std;
using boost::spirit;
struct ex : public grammar<route_grammar> {
  template <typename ScannerT> struct defintion {
    definition(ex const& self) {
      expression = real_p; 
    }
    rule<ScannerT> expression;
    rule<ScannerT> const& start() const { return expression; }
};

int main() {
  file_iterator<char> first;
  file_iterator<char> last = first.make_end();
  ex ex_p;
  parse_info<file_iterator<char> > info = parse(first, last, ex_p, space_p);
  return 0;
}
#包括
#包括
#包括
#包括
使用性病;
使用精神;
结构ex:公共语法{
模板结构定义{
定义(ex const&self){
表达式=实际值;
}
规则表达;
规则常量&start()常量{return expression;}
};
int main(){
首先是文件迭代器;
file_iterator last=first.make_end();
前苏联;
parse_info=parse(first,last,ex_p,space_p);
返回0;
}

此代码因以下错误而中断:错误:无法在参数传递中将
const boost::spirit::file_iterator
转换为
const char*

,下面是一种获取char*指向与迭代器相同元素的方法:

&v.front() // v.begin()
&v.back() + 1 // v.end()
但我不确定你是如何编译的:

vector<char> v;
v.push_back("3.2");
向量v; v、 推回(“3.2”);
很难从发布的代码中分辨出来,因为它包含一些基本错误。 纠正这些错误后,它在我的机器上(使用MSVC++7.1)编译得很好:

#包括
#包括
#包括
使用名称空间std;
使用名称空间boost::spirit;
结构ex:公共语法{
模板
结构定义{
定义(ex const&self)
{
表达式=实际值;
}
规则表达;
规则常量&start()常量{return expression;}
};
};
int main(){
向量v;
v、 推回('3');v.推回('.');v.推回('2');
前苏联;
parse_info=parse(v.begin(),v.end(),ex_p,space_p);
返回0;
}

编译错误不在提供的示例中。在上面的语法中,我没有语义操作,而在代码中,我简化了


这些操作的回调使用char*,而不是我使用的迭代器类型。

您可以尝试确保您的语义操作在参数类型上是多态的。在代码中,您可能希望这样:

struct my_action {
  template <class ParamType>
  void operator()(ParamType const & parameter) const {
    // deal with parameter
  }
};
或者,如果您需要二进制语义操作,您可以执行以下操作:

struct binary_action {
  template <class ParamType>
  void operator()(ParamType const & param1, ParamType const & param2) const {
    // deal with either parameter
  }
};

(*char_p)[binary_action()]
struct二进制\u操作{
模板
void运算符()(参数类型常量和参数1,参数类型常量和参数2)常量{
//处理任一参数
}
};
(*char\u p)[二进制操作()]

我已经修复了您在代码中指出的错误。此外,我无法转换为C样式的字符串。这是一条有用的信息。我想这一定是编译器版本问题。
real_p[my_action()]
struct binary_action {
  template <class ParamType>
  void operator()(ParamType const & param1, ParamType const & param2) const {
    // deal with either parameter
  }
};

(*char_p)[binary_action()]