【问题标题】:Boost Spirit v2 gcc compilation error which does not show up using msvcBoost Spirit v2 gcc 编译错误,使用 msvc 未显示
【发布时间】:2011-01-31 20:01:28
【问题描述】:

我最近在 windows 中编写了一些精神解析代码,我最近尝试在 ubuntu 机器上构建它并遇到了一个我正在努力解决的编译错误。

经过一些黑客攻击和削减,我设法提出了这个示例代码 sn-p,它表现出相同的行为:

struct FooParser
: spirit::qi::grammar<
    std::string::const_iterator, 
    double(), 
    spirit::qi::ascii::space_type>
{
    FooParser() : FooParser::base_type(a_rule)
    {
        using namespace boost::spirit::qi;
        a_rule = double_;
    }

    spirit::qi::rule<
        string::const_iterator,
        double(),
        spirit::qi::ascii::space_type> a_rule;
};

然后像这样传递给一个短语解析:

double result;
std::string txt;
FooParser foobar;
//...
if(phrase_parse(txt.begin(), txt.end(), foobar, space, result))
{
//do something
}

并且在编译时,会产生如下错误:

boost/spirit/home/qi/reference.hpp:41: error: no matching function for call to 
‘boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char,
 std::char_traits<char>, std::allocator<char> > >, double(), 
boost::proto::exprns_::expr<boost::proto::tag::terminal, 
boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
boost::spirit::char_encoding::ascii> >, 0l>, boost::fusion::unused_type, 
boost::fusion::unused_type>::parse(__gnu_cxx::__normal_iterator<char*, 
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, const 
__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >&, const boost::fusion::unused_type&, const 
boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, 
boost::spirit::char_encoding::ascii> >&, double&) const’

让我感到沮丧的是,Visual Studio 似乎非常乐意编译和运行代码。我希望聪明的互联网能告诉我我犯了什么错误。

【问题讨论】:

    标签: c++ gcc boost


    【解决方案1】:

    我认为这里的问题是std::stringbegin()end(),试试这个:

    std::string::const_iterator begin = txt.begin();
    std::string::const_iterator end = txt.end();
    

    然后传入:

    phrase_parse(begin, end, foobar, space, result)
    

    我认为问题在于,您在其他任何地方都使用 const_iterator 类型,但在非 const 字符串上的 begin()end() 返回一个正常的迭代器。

    错误的关键部分是这个位:

    parse(__gnu_cxx::__normal_iterator, std::allocator > >&, const __gnu_cxx::__normal_iterator, std::allocator > >&

    【讨论】:

    • 啊,你是我的英雄!很遗憾,经过这么长时间,我仍然不了解 C++ 中的类型/常量提升。另一方面,我猜 MSVC 也不理解它,所以我不应该感到那么糟糕。
    • hehehe.. 问题是这些简单的事情隐藏在极其复杂的错误消息中,需要一段时间才能理解发生了什么...... :) 我过去做过(并且我自己仍然继续这样做 - 如果这是任何安慰的话!):)
    • @Voltaire:我永远不会对 const 正确性能够捕获的编程错误数量感到惊讶。
    • string::iterator 派生自 VC++ 库中的 string::const_iterator,但它不在 gcc 中,至少在我拥有的库中没有 - 因此你看到的结果。我想根据你的喜好,你可以认为这是 VC++ 不必要地迎合了 gcc 用户认为 VC++ 迎合的普通人,或者默认的 gcc 库像标准允许的那样绝对令人讨厌(但没有更多,当然! ),如您所见;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 2015-01-25
    • 2011-07-23
    • 2015-05-13
    相关资源
    最近更新 更多