gpt4 book ai didi

c++ - Boost Spirit X3 在将字符串填充到 vector 时没有匹配的调用引用

转载 作者:行者123 更新时间:2023-11-30 03:22:26 26 4
gpt4 key购买 nike

你好,我正在尝试使用 spirit x3 将字符串填充到 vector 中,但出现以下错误。代码直接来自文档,除了 vector 使用字符串。

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)

Note: Bugs

If you have Boost 1.65-1.66, you'll run into How to make a recursive rule in boost spirit x3 in VS2017, (Live On Wandbox which was fixed in Boost 1.67 (and earlier too, by the way, e.g. Live on Wandbox/Boost 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;
}

关于c++ - Boost Spirit X3 在将字符串填充到 vector 时没有匹配的调用引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51150601/

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