提升精神:错误C2664,无法将'const boost :: phoenix :: actor <eval>'转换为'char'</eval>

时间:2011-07-01 15:10:58

标签: c++ json parsing boost boost-spirit

我是Boost Spirit的新手,并尝试使用Boost Spirit 2.4.2(Boost 1.46.1)编写JSON解析器。对于下面的代码,我在尝试执行语义操作/属性时收到错误:

Error   1   error C2664: 'void (char)' : cannot convert parameter 1 from 'const boost::phoenix::actor<Eval>' to 'char'

我看到了一些问题,但它们并不适用于我的案例。请帮忙。谢谢!

#include <map>
#include <string>
#include <vector>
#include <iostream>

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_container.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/fusion/include/adapt_assoc_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

void print_char(char c)
{
    std::cout << c;
}

template <typename Iterator>
struct json_grammar : qi::grammar<Iterator, ascii::space_type>
{
    json_grammar() : json_grammar::base_type(start)
    {
        using ascii::alpha;
        using ascii::alnum;
        using qi::long_long;
        using qi::long_double;
        using qi::lit;
        using qi::char_;
        using qi::lexeme;
        typedef boost::function<void(char)> char_action_t;

        //char_action_t beginObj  (boost::bind(&print_char, qi::_1));

        // 
        start =
            char_('{')          [boost::bind(&print_char, qi::_1)]
            >> -(js_member % ',')
            >> char_('}')
        ;
        // 
        js_member =
            js_key
            >> ':' >> js_value
        ;
        // 
        js_key = (alpha >> *alnum) | js_string
        ;
        // 
        js_string = js_single_quoted_str | js_double_quoted_str
        ;
        // 
        js_array = lit('[') >> -(js_value % ',') >> lit(']')
        ;
        // 
        js_bool = lit("true") | lit("false")
        ;
        // 
        js_null = lit("null")
        ;
        // 
        js_value = js_string | js_num | js_array | start | js_bool | js_null | js_empty_str;
        // 
        js_single_quoted_str = (lexeme["'" >> +((char_ | ' ' | "\t") - "'") >> "'"]);
        // 
        js_double_quoted_str = (lexeme['"' >> +((char_ | ' ' | "\t") - '"') >> '"']);
        // 
        js_empty_str = lit("''") | lit("\"\"");
        // 
        js_num = long_long | long_double;
    }

    qi::rule<Iterator, ascii::space_type> start;
    qi::rule<Iterator, ascii::space_type> js_member;
    qi::rule<Iterator, ascii::space_type> js_key;
    qi::rule<Iterator, ascii::space_type> js_value;
    qi::rule<Iterator, ascii::space_type> js_string;
    qi::rule<Iterator, ascii::space_type> js_single_quoted_str;
    qi::rule<Iterator, ascii::space_type> js_double_quoted_str;
    qi::rule<Iterator, ascii::space_type> js_empty_str;
    qi::rule<Iterator, ascii::space_type> js_array;
    qi::rule<Iterator, ascii::space_type> js_num;
    qi::rule<Iterator, ascii::space_type> js_null;
    qi::rule<Iterator, ascii::space_type> js_bool;
};

int main()
{
    std::string inputStr;
    json_grammar<std::string::const_iterator> jsonParser;
    bool parseOK = false;

    while(std::getline(std::cin, inputStr)) {
        if(inputStr.empty() || inputStr[0] == 'q' || inputStr[0] == 'Q')
            break;

        std::string::const_iterator iter = inputStr.begin();
        std::string::const_iterator iterEnd = inputStr.end();

        parseOK = qi::phrase_parse(iter, iterEnd, jsonParser, ascii::space);

        if(parseOK && iter == iterEnd) {
            std::cout << "Successfully parsed the input as JSON!" << std::endl;
        } else {
            std::cout << "Cannot parse the input as JSON!" << std::endl;
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:3)

更新更新:

Kostya有一个很好的观点,'half'触及基础:boost::bind实际上很好,只需使用不同的占位符:

[ boost::bind(&print_char, ::_1) ]
[ phoenix::bind(&print_char, qi::_1) ]
[ print_char ]

这三个都应该有效,但不能混合:)

答案 1 :(得分:1)

似乎boost::bind与精神一起使用并不是很好。我记得建议使用phoenix代替。我已经用以下代码替换了这一行:

   char_('{') [std::cout << boost::phoenix::arg_names::arg1]

它编译。没时间检查它是否确实有效。