【问题标题】:error compiling Spirit sample编译 Spirit 示例时出错
【发布时间】:2012-02-20 21:40:49
【问题描述】:

this 其他问题的公认答案将我带到this 示例,但编译它会给出一个很长的错误列表。这里是示例代码,我只添加了包含和一个虚拟 main():

#include <boost/spirit/include/qi.hpp>
#include <vector>
#include <map>
#include <string>
#include <iostream>

namespace qi = boost::spirit::qi;

template <typename Iterator>
struct keys_and_values
  : qi::grammar<Iterator, std::map<std::string, std::string>()>
{
    keys_and_values()
      : keys_and_values::base_type(query)
    {
        query =  pair >> *((qi::lit(';') | '&') >> pair);
        pair  =  key >> -('=' >> value);
        key   =  qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
        value = +qi::char_("a-zA-Z_0-9");
    }
    qi::rule<Iterator, std::map<std::string, std::string>()> query;
    qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
    qi::rule<Iterator, std::string()> key, value;
};

int main(int argc, char **argv)
{
    std::string input("key1=value1;key2;key3=value3");  // input to parse
    std::string::iterator begin = input.begin();
    std::string::iterator end = input.end();

    keys_and_values<std::string::iterator> p;    // create instance of parser
    std::map<std::string, std::string> m;        // map to receive results
    bool result = qi::parse(begin, end, p, m);   // returns true if successful
}

我已经尝试过 boost 1.42(我的 Ubuntu 11.04 发行版的默认版本)和 1.48(已下载)。错误(我报告由 QtCreator 过滤的错误)不同:ver 1.42 给出

/usr/include/boost/fusion/support/tag_of.hpp:92:13: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::not_<boost::fusion::detail::is_specialized<std::pair<std::basic_string<char>, std::basic_string<char> > > >::************)’

/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(std::pair<std::basic_string<char>, std::basic_string<char> >&)’

/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(mpl_::void_&)’

同时版本。 1.48 给了

/home/carlo/Projects/spirit_vect_literals-build-desktop/../../cpp/boost_1_48_0/boost/spirit/home/qi/detail/assign_to.hpp:123: error: no matching function for call to ‘std::pair<std::basic_string<char>, std::basic_string<char> >::pair(const std::basic_string<char>&)’

我有什么遗漏吗?

编辑:我找到了解决方案:添加此标头并编译两个版本

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

【问题讨论】:

    标签: c++ boost boost-spirit std-pair


    【解决方案1】:

    恭喜您找到了这个问题...几周前我遇到了同样的错误,并且浪费了几个小时。如您所见,解决方案就是包含以下内容:

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

    它为 Qi 使用 std::pair 作为规则的输出提供了必要的魔法。

    我主要将这个答案留在这里,这样问题就不再显示为未回答 - 如果你想添加/接受你自己的答案,我会收回这个。

    【讨论】:

    • 是的,这是一个严重的常见问题解答,但公平地说,那篇文章是 a comment by Thomas Heller :)
    • @sehe:我发现那一行又一行的大量阅读和许多尝试(部分成功,但通常令人沮丧)制作我的超小的测试用例。我不敢相信 include 可以解决我的问题。让我伤心的是,文档隐藏了 Hartmut Kaiser 清楚说明的简单性......
    • @chac 是的,很遗憾该页面由社区维护,否则我会亲自编辑其中的#include。
    猜你喜欢
    • 2011-07-23
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多