编译 Spirit 样本时出错
这个的可接受答案 其他问题引导我this 示例,但编译它会给出一个很长的错误列表。在示例代码中,我仅添加了包含文件和虚拟 main():
#include <boost/spirit/include/qi.hpp>
#include <vector>
#include <map>
#include <string>
#include <iostream>
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct keys_and_values
: qi::grammar<Iterator, std::map<std::string, std::string>()>
{
keys_and_values()
: keys_and_values::base_type(query)
{
query = pair >> *((qi::lit(';') | '&') >> pair);
pair = key >> -('=' >> value);
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
value = +qi::char_("a-zA-Z_0-9");
}
qi::rule<Iterator, std::map<std::string, std::string>()> query;
qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
qi::rule<Iterator, std::string()> key, value;
};
int main(int argc, char **argv)
{
std::string input("key1=value1;key2;key3=value3"); // input to parse
std::string::iterator begin = input.begin();
std::string::iterator end = input.end();
keys_and_values<std::string::iterator> p; // create instance of parser
std::map<std::string, std::string> m; // map to receive results
bool result = qi::parse(begin, end, p, m); // returns true if successful
}
我尝试了 boost 1.42(在我的 Ubuntu 11.04 发行版上默认)和 1.48(已下载)。错误(我报告由 QtCreator 过滤的错误)不同:版本 1.42 给出,
/usr/include/boost/fusion/support/tag_of.hpp:92:13: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::not_<boost::fusion::detail::is_specialized<std::pair<std::basic_string<char>, std::basic_string<char> > > >::************)’
/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(std::pair<std::basic_string<char>, std::basic_string<char> >&)’
/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(mpl_::void_&)’
而版本 1.42 给出。 1.48 给出
/home/carlo/Projects/spirit_vect_literals-build-desktop/../../cpp/boost_1_48_0/boost/spirit/home/qi/detail/assign_to.hpp:123: error: no matching function for call to ‘std::pair<std::basic_string<char>, std::basic_string<char> >::pair(const std::basic_string<char>&)’
我缺少什么吗?
编辑:我找到了解决方案:添加此标头并且两个版本都可以编译
#include <boost/fusion/adapted/std_pair.hpp>
The accepted answer to this other question lead me to this sample, but compiling it give a long error list. Here the sample code, I added just the includes and a dummy main():
#include <boost/spirit/include/qi.hpp>
#include <vector>
#include <map>
#include <string>
#include <iostream>
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct keys_and_values
: qi::grammar<Iterator, std::map<std::string, std::string>()>
{
keys_and_values()
: keys_and_values::base_type(query)
{
query = pair >> *((qi::lit(';') | '&') >> pair);
pair = key >> -('=' >> value);
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
value = +qi::char_("a-zA-Z_0-9");
}
qi::rule<Iterator, std::map<std::string, std::string>()> query;
qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
qi::rule<Iterator, std::string()> key, value;
};
int main(int argc, char **argv)
{
std::string input("key1=value1;key2;key3=value3"); // input to parse
std::string::iterator begin = input.begin();
std::string::iterator end = input.end();
keys_and_values<std::string::iterator> p; // create instance of parser
std::map<std::string, std::string> m; // map to receive results
bool result = qi::parse(begin, end, p, m); // returns true if successful
}
I've tried both boost 1.42 (default on my Ubuntu 11.04 distro), and 1.48 (downloaded). Errors (I report those filtered by QtCreator) differ: ver 1.42 gives
/usr/include/boost/fusion/support/tag_of.hpp:92:13: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::not_<boost::fusion::detail::is_specialized<std::pair<std::basic_string<char>, std::basic_string<char> > > >::************)’
/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(std::pair<std::basic_string<char>, std::basic_string<char> >&)’
/usr/include/boost/spirit/home/support/attributes.hpp:409: error: no matching function for call to ‘std::basic_string<char>::basic_string(mpl_::void_&)’
while ver. 1.48 gives
/home/carlo/Projects/spirit_vect_literals-build-desktop/../../cpp/boost_1_48_0/boost/spirit/home/qi/detail/assign_to.hpp:123: error: no matching function for call to ‘std::pair<std::basic_string<char>, std::basic_string<char> >::pair(const std::basic_string<char>&)’
Do I have something missing?
edit: I've found the solution: add this header and both versions compile
#include <boost/fusion/adapted/std_pair.hpp>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恭喜您找到了这个问题...几周前我遇到了同样的错误并浪费了几个小时。正如您所发现的,解决方案只是包含以下内容:
它为 Qi 使用 std::pair 作为规则的输出提供了必要的魔力。
我主要将这个答案留在这里,以便问题不再显示为未回答 - 如果您想添加/接受您自己的答案,我将撤回此答案。
Congratulations on tracking this down... I hit the same error a few weeks back and wasted hours with it. As you found, the solution is just to include this:
which provides the necessary magic for Qi to use std::pair as the output of a rule.
I'm mainly leaving this answer here just so the question no longer shows up as unanswered - if you want to add/accept your own answer I'll retract this.