示例解析错误

Example parsing error(示例解析错误)

本文介绍了示例解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照示例解析提升精神 (2.5.2) 的示例.我的代码如下

I'm trying to parse an example of boost spirit (2.5.2) following the example. My code is the following

#include <boostspirithomeqi.hpp>
#include <iostream>
#include <string>
#include <utility>

int main()
{

  // Parsing two numbers
  std::string input("1.0 2.0");
  std::pair<double, double> p;

  boost::spirit::qi::phrase_parse(
    input.begin(),
    input.end(),
    boost::spirit::qi::double_ >> boost::spirit::qi::double_ , // Parse grammar
    boost::spirit::qi::space,
    p
  );

  return 0;
}

它几乎等于找到的例子 此处,但是当我使用 Visual Studio 2010(32 位,调试)编译它时,出现以下错误:

It's almost equal to the example found here, but when I compile it with Visual studio 2010 (32 bit, debug) I obtain the following error:

error C2440: 'static_cast': unable to convert from 'const double' to 'std::pair<_Ty1,_Ty2>'

(错误可能略有不同,我是从意大利语翻译过来的)

(the error can be slighty different, I've translated it from italian)

我做错了什么,我怎样才能成功编译这个例子?

What I'm doing wrong and how can I compile successfully the example?

预先感谢您的回复.

推荐答案

您缺少一个包含:

#include <boost/fusion/adapted/std_pair.hpp>

它定义了属性分配规则,使 Fusion 序列 (vector2<>) 可分配给 std::pair.

It defines the attribute assignment rules to make Fusion sequences (vector2<>) assignable to std::pair.

查看实时代码:liveworkspace.org

See the code live: liveworkspace.org

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <iostream>
#include <string>
#include <utility>

int main()
{
    // Parsing two numbers
    std::string input("1.2 3.4");
    std::pair<double, double> p;

    namespace qi = boost::spirit::qi;

    qi::phrase_parse(
            input.begin(), 
            input.end(),
            qi::double_ >> qi::double_ , // Parse grammar
            qi::space, p);

    std::cout << "Lo:     " << p.first << "
"
              << "Behold: " << p.second << "
";
}

这篇关于示例解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:示例解析错误

基础教程推荐