gpt4 book ai didi

c++ - 解析为类(不是结构)

转载 作者:行者123 更新时间:2023-11-30 00:49:59 25 4
gpt4 key购买 nike

下面我展示了一个编辑过的 spirits employee 示例,它没有编译。我要解决的问题是解析为类而不是结构。我知道,除了公共(public)/私有(private)之外,它完全相同。但是在将类/结构存储到 vector 中之前,我需要有一个构造函数来工作。如何更改 BOOST_FUSION_ADAPT_STRUCT?

我怎样才能让它运行?

// STD HEADER
#include <iostream>
#include <string>
#include <complex>

// BOOST HEADER
#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_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

namespace client
{
namespace qi = boost::spirit::qi;

namespace ascii = boost::spirit::ascii;

class employee
{
public:
employee (
int _age
, std::string _surname
, std::string _forename
, double _salary
);

private:
int age_;
std::string surname_;
std::string forename_;
double salary_;
};

employee::employee (
int _age
, std::string _surname
, std::string _forename
, double _salary
) : age_(_age)
, surnemae_(_surename)
, forename_(_forename)
, double salary_
{
// do some important stuff
}

}

// WHAT TO DO HERE?
BOOST_FUSION_ADAPT_STRUCT(
client::employee
)

namespace client
{
template <typename Iterator>
struct employee_parser
: qi::grammar<Iterator, employee(), ascii::space_type>
{
employee_parser() : employee_parser::base_type(start)
{
using qi::int_;
using qi::lit;
using qi::double_;
using qi::lexeme;
using ascii::char_;

quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];

start %=
lit("employee")
>> '{'
>> int_ >> ','
>> quoted_string >> ','
>> quoted_string >> ','
>> double_
>> '}'
;
}

qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, employee(), ascii::space_type> start;
};
}

int main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "\t\tAn employee parser for Spirit...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";

std::cout
<< "Give me an employee of the form :"
<< "employee{age, \"surname\", \"forename\", salary } \n";
std::cout << "Type [q or Q] to quit\n\n";

using boost::spirit::ascii::space;
typedef std::string::const_iterator iterator_type;
typedef client::employee_parser<iterator_type> employee_parser;

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

client::employee emp;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, g, space, emp);

if (r && iter == end)
{
std::cout << boost::fusion::tuple_open('[');
std::cout << boost::fusion::tuple_close(']');
std::cout << boost::fusion::tuple_delimiter(", ");

std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "got: " << boost::fusion::as_vector(emp) << 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;
}

最佳答案

你可以

1。适应为ADT

通过添加 getter/setter。另一个答案链接到这个作为解决方案:http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/adapted/adapt_adt.html

2。使用语义 Action :

查看 Live On Coliru

变化:

  1. 使类默认可构造
  2. 从规则的语义操作中显式调用构造函数:

        start =
    lit("employee")
    >> ('{'
    >> int_ >> ','
    >> quoted_string >> ','
    >> quoted_string >> ','
    >> double_
    >> '}'
    ) [ qi::_val = boost::phoenix::construct<employee>(qi::_1, qi::_2, qi::_3, qi::_4) ]
    ;
  3. 可选择重载 operator<<所以你仍然可以打印类进行调试

3。提供定制点

参见 http://www.boost.org/doc/libs/1_56_0/libs/spirit/doc/html/spirit/advanced/customize/assign_to.html

关于c++ - 解析为类(不是结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26527853/

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