题
当我尝试使用解析出一个灵语法的迭代形式我从迭代器类型传递转换错误为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;
}
此代码中断与:错误:无法转换const boost::spirit::file_iterator<char_t, boost::spirit::fileiter_impl::mmap_file_iterator<char_t> >
在争论const char*
传递
解决方案
很难从代码告诉张贴,因为它包含了一些基本的错误。 这些校正之后,它编译我的机器(用MSVC ++ 7.1)上精细:
#include <boost/spirit/core.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace boost::spirit;
struct ex : public grammar<ex> {
template <typename ScannerT>
struct definition {
definition(ex const& self)
{
expression = real_p;
}
rule<ScannerT> expression;
rule<ScannerT> const& start() const { return expression; }
};
};
int main() {
vector<char> v;
v.push_back('3'); v.push_back('.'); v.push_back('2');
ex ex_p;
parse_info<vector<char>::iterator> info = parse(v.begin(), v.end(), ex_p, space_p);
return 0;
}
其他提示
下面是一种方法获得的char *的指示在相同的元件作为迭代器:
&v.front() // v.begin()
&v.back() + 1 // v.end()
我不知道你是如何得到这虽然编译:
vector<char> v;
v.push_back("3.2");
在编译错误是不设置于样品中。在语法上面我没有语义动作,而在代码中,我简化我做到了。
我使用针对使用的炭这些行动的回调*代替迭代器类型。
您可以尝试确保你的语义动作是对参数的类型多态。正是在代码中你想要的东西是这样的:
struct my_action {
template <class ParamType>
void operator()(ParamType const & parameter) const {
// deal with parameter
}
};
您会使用这样的一元语义动作,如下所示:
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()]
不隶属于 StackOverflow