c++ - 将字符串填充到向量时,Boost Spirit X3 没有匹配的调用引用
问题描述
您好,我正在尝试使用 Spirit x3 将字符串填充到向量中,但出现以下错误。代码直接来自文档,除了向量使用字符串。
error: no matching function for call to
std::vector<std::__cxx11::basic_string<char>
>::push_back(boost::spirit::x3::unused_type&)’
auto push_back = [&](auto& ctx){ slt.push_back(_attr(ctx)); };`
我的代码看起来像这样,我认为所有必要的包含都在上面(它在类方法中):
#include <boost/spirit/home/x3.hpp>
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;
using x3::double_;
using x3::phrase_parse;
using x3::_attr;
using x3::parse;
using x3::lit;
using x3::char_;
using x3::lexeme;
using x3::alpha;
using x3::alnum;
using x3::skip;
using ascii::space;
/*Something,something.......*/
auto name = x3::rule<class name>{}
= char_("a-zA-Z") >> *char_("a-z_A-Z0-9");
auto args_l = x3::rule<class l>{}
= " " >> (name % skip(space)[","]);
auto comment = x3::rule<class comment>{}
= "//" >> *char_;
auto iter_start = line.begin();
auto iter_end = line.end();
vector<string> slt;
auto push_back = [&](auto& ctx){ slt.push_back(_attr(ctx)); };
bool result = parse(
iter_start,
iter_end,
name[push_back] >> -args_l >> *(char_(" "))
);
/某事,某事....... /
解决方案
您的规则定义不公开属性。
就像@llonesmiz 指出的那样,解决这个问题:
auto name = x3::rule<class name, std::string>{}
= char_("a-zA-Z") >> *char_("a-z_A-Z0-9");
看到它Live On Wandbox (boost-1.67)
注意:错误
如果你有 Boost 1.65-1.66,你会遇到 How to make a recursive rule in boost spirit x3 in VS2017,(Live On Wandbox 已在 Boost 1.67 中修复(顺便说一下,更早的版本,例如Live on Wandbox/提升 1.64 )
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
namespace P {
using namespace x3;
auto name = x3::rule<class name, std::string>{}
= char_("a-zA-Z") >> *char_("a-z_A-Z0-9");
auto args_l = x3::rule<class l>{}
= " " >> (name % skip(space)[","]);
auto comment = x3::rule<class comment>{}
= "//" >> *char_;
}
#include <iostream>
#include <iomanip>
int main() {
std::string const line = "a90_b";
auto iter_start = line.begin();
auto iter_end = line.end();
std::vector<std::string> slt;
auto push_back = [&](auto& ctx){ slt.push_back(x3::_attr(ctx)); };
bool result = parse(
iter_start,
iter_end,
P::name[push_back] >> -P::args_l >> *x3::char_(" ")
);
for (auto& tok: slt) {
std::cout << std::quoted(tok) << "\n";
}
if (iter_start!=iter_end)
std::cout << "Remaining unparsed: " << std::quoted(std::string(iter_start, iter_end)) << "\n";
return result? 0 : 255;
}
推荐阅读
- android - 如何在图形布局android studio中的Attributes下添加字体Family属性
- kotlin - 多少协程太多了?
- python-3.x - 第 4 行文件 firstday.py 中以 '\x85' 开头的非 UTF-8 代码,但未声明编码
- excel - 使用 VBA 将 dd.mm.yyyy 转换为英国日期格式的问题
- javascript - 如何制作影响角度不同数据的可重用函数?
- html - 在里面放一个按钮使用 pug.js 标记
- spring-boot - 如何使用 Spring Webflux 返回 GZIP 响应?
- excel - 合并 2 个工作表并多次复制工作表 1 的所有行
- facebook - 带有 Facebook 的 Azure Web App Bot Webhook 文本始终放在 V4 上
- google-api - 为什么 Google API for QR 码现在不起作用?