gpt4 book ai didi

c++ - 提升灵气规则属性问题

转载 作者:行者123 更新时间:2023-11-30 04:33:40 24 4
gpt4 key购买 nike

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

#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

有没有人可以给点建议?谢谢

最佳答案

这已在其他地方进行了详细讨论(例如 here )。如果您将打印功能更改为 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;

关于c++ - 提升灵气规则属性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6627716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com