1

我有以下代码无法编译,但是我不知道问题出在哪里:

#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_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/foreach.hpp>

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

void print (std::string& s)
{
    std::cout << "Get string [" << s << ']' << std::endl;
}

namespace client
{
    namespace fusion = boost::fusion;
    namespace phoenix = boost::phoenix;
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    template <typename Iterator>
    struct my_grammar
      : qi::grammar<Iterator, std::string(), ascii::space_type>
    {
        my_grammar()
          : my_grammar::base_type(start)
        {
        using qi::lit;
        using qi::int_;
        using qi::lexeme;
        using ascii::char_;
        using ascii::string;
        using ascii::space_type;
        using namespace qi::labels;


        // Here you can see the rule oct_char with attribute type as char
        // +(char) would be vector<char> which is compactable with string type
        // Why the compiler still told me the type of the attribute is not right?
        start %= lexeme[+(oct_char)] [&print]
            ;

        oct_char =
                   lexeme["//"        
                           >>  char_('0','3') [ _a = ( _1 - '0' ) ]
                     ||     char_('0','7') [ _a = _a << 3 + ( _1 - '0' ) ]
                ||     char_('0','7') [ _a = _a << 3 + ( _1 - '0' ) ]]
                 [ _val = _a ]
                ;
        }
    qi::rule<Iterator, std::string(), ascii::space_type> start;
        qi::rule<Iterator, char(), qi::locals<char>, ascii::space_type> oct_char;

    };
}

int main(int argc, char **argv)
{

    typedef client::my_grammar<std::string::const_iterator> Grammer;
    Grammer grm;

    std::string str;


using boost::spirit::ascii::space;

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

    if (phrase_parse(str.begin(), str.end(), grm, space))
    {
        std::cout << "-------------------------\n";
        std::cout << "Parsing succeeded\n";
        std::cout << "got: " << str << std::endl;
        std::cout << "\n-------------------------\n";
    }
    else
    {
        std::cout << "-------------------------\n";
        std::cout << "Parsing failed\n";
        std::cout << "-------------------------\n";
    }
}

std::cout << "Bye... :-) \n\n";
return 0;


}

以下是错误消息:

Error   2   error C2664: 'void (std::basic_string<_Elem,_Traits,_Ax> )' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::basic_string<_Elem,_Traits,_Ax> ' d:\code\boost_1_46_1\boost\spirit\home\support\action_dispatch.hpp  70

有没有人可以提供一些建议?谢谢

4

1 回答 1

2

这已在别处(例如此处)详细讨论过。如果您将打印功能更改为 a vector<char>,它将起作用:

void print (std::vector<char> const& s)
{
    std::cout << "Get string [" 
              << std::string(s.begin(), s.end()) << ']' 
              << std::endl;
}

不过,您的代码中还有一个错误。该规则oct_char不需要有船长,因为它在内部使用lexeme[]

qi::rule<Iterator, char(), qi::locals<char> > oct_char;
于 2011-07-09T01:05:10.703 回答