我试图解析一个可选的事物列表,后面跟着一个分号(这是一个简化的例子)。下面是一个示例程序:
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
namespace x3=boost::spirit::x3;
namespace ast
{
struct doubleOrString : x3::variant<double,std::string>{
using base_type::base_type;
using base_type::operator=;
};
struct pass {
std::vector<doubleOrString> dOrs;
};
}//ast namespace
BOOST_FUSION_ADAPT_STRUCT(ast::pass,dOrs)
x3::rule<class doubleOrString_class, ast::doubleOrString> const doubleOrString = "doubleOrString";
x3::rule<class pass_class, ast::pass> const pass = "pass";
x3::rule<class es_class, std::optional<ast::pass>> const es = "es";
auto const doubleOrString_def =x3::double_ | x3::string("AString");
auto const pass_def = doubleOrString % x3::lit(",");
auto const es_def = (pass > x3::lit(";")) | x3::lit(";");
BOOST_SPIRIT_DEFINE(doubleOrString,pass,es);
int main()
{
using boost::spirit::x3::ascii::space;
typedef std::string::const_iterator iterator_type;
std::string str;
while (getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::optional<ast::pass> esdOrs;
iterator_type iter = str.begin();
iterator_type const end = str.end();
bool r = phrase_parse(iter, end, es, space, esdOrs);
if (r && iter == end)
{
std::cout << "\nGood!\n";
}
else
{
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}但是,它无法使用错误进行编译:
usr/include/boost/spirit/home/x3/support/ast/variant.hpp:184:17:错误:不匹配“operator=”(操作数类型为“boost::spirit::x3::variant
r> >>::variant_type‘{aka’boost::variant}和‘std::optionalclient::ast::pass’)
26 184 var = std::forward(rhs);
我做错了什么?这个错误信息不多。如果我只是做一些可选的事情,它似乎会很好。
我使用以下命令(文件名为r1.cpp):
g++ --std=c++17 r1.cpp -o r1 发布于 2022-09-11 18:56:37
使用以下MRE(最小可重现性示例):
#include <vector>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
namespace x3=boost::spirit::x3;
namespace ast
{
struct doubleOrString : x3::variant<double,std::string>{
using base_type::base_type;
using base_type::operator=;
};
struct pass {
std::vector<doubleOrString> dOrs;
};
}//ast namespace
BOOST_FUSION_ADAPT_STRUCT(ast::pass,dOrs)
x3::rule<class doubleOrString_class, ast::doubleOrString> const doubleOrString = "doubleOrString";
x3::rule<class pass_class, ast::pass> const pass = "pass";
x3::rule<class es_class, std::optional<ast::pass>> const es = "es";
auto const doubleOrString_def =x3::double_ | x3::string("AString");
auto const pass_def = doubleOrString % x3::lit(",");
auto const es_def = (pass > x3::lit(";")) | x3::lit(";");
BOOST_SPIRIT_DEFINE(doubleOrString,pass,es);我没有编译器错误。请提供显示错误的MRE。
https://stackoverflow.com/questions/73673899
复制相似问题