c++ - 带有可选解析器的 Boost.Qi 编译器错误

标签 c++ boost boost-spirit

我是 Boost.Qi 的初学者,所以我正在尝试一些简单的示例来尝试理解它。我正在尝试解析如下所示的字符串:

A:1     B:2           C:3

字符串中每个组件之间有任意数量的空格。 A: 等部分是固定的,我想解析整数值。字符串的第三个组成部分,即上例中的 C:3,是可选的。我提出了以下简单示例来测试此应用程序的 Boost.Qi:

#include <boost/optional.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>

namespace qi = boost::spirit::qi;

int main()
{
    std::string s = "A:1   B:2          C:3";

    int a, b;
    boost::optional<int> c;

    if (!qi::parse(s.begin(), s.end(),
        qi::lit("A:") >> qi::int_ >> +qi::space >> "B:" >> qi::int_ >> 
            -(+qi::space >> "C:" >> qi::int_), a, b, c))
    {
        std::cout << "failed to parse" << std::endl;
    }

    std::cout << a << ' ' << b << ' ' << c.value_or(-1) << std::endl;
}

但是,这无法编译(在 C++11 模式下使用 Boost v1.58 和 g++ 5.4.0)。在典型的 C++ 模板错误消息海洋中,我发现以下内容:

spirit.cc:15:55:   required from here
/usr/include/boost/spirit/home/support/container.hpp:130:12: error: ‘int’ is not a class, struct, or union type
     struct container_value

spirit.cc:15:55:   required from here
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:316:66: error: no type named ‘type’ in ‘struct boost::spirit::traits::container_value<int, void>’
             typedef typename traits::container_value<Attr>::type value_type;
                                                                  ^
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:329:15: error: no type named ‘type’ in ‘struct boost::spirit::traits::container_value<int, void>’

我在这里做错了什么对任何人来说都是显而易见的吗?

最佳答案

解决这个问题的方法是使用 phrase_parseqi::space skipper:

if (!qi::phrase_parse(s.begin(), s.end(),
        qi::lit("A:") >> qi::int_ >> "B:" >> qi::int_ >> -("C:" >> qi::int_), qi::space, a, b, c))

关于c++ - 带有可选解析器的 Boost.Qi 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40366608/

相关文章:

c++ - 包装在 C++/CLI 中使用 native 接口(interface)的 native C++ 代码

c++ - 检查值是 int 还是 string 时程序陷入循环

c++ - C 样式转换导致 SIGILL,dynamic_cast ok

c++ - 如何使用 boost lambda 在集合中的每个元素上调用方法?

c++ - boost::iostream bzip2_decompressor 不解压缩由 bzip2_compressor 压缩的文件

visual-studio-2010 - 如何编译使用 boost 库的 DLL?

boost-spirit - 使用 boost Spirit 解析负十六进制值

c++ - 如何在没有预标记的情况下解析异构列表?

c++ - Boost Spirit 2 - 符号扩展和回溯

c++ char数组空字符while循环