我正在尝试将 Unix 文件路径解析为文件夹对(键/值对 - 根据我的应用程序的需要)。问题是我无法正确定义使用 qi::rule 的解析器。我得到了我无法完全理解的编译器错误页面(对不起,如果问题听起来很简单,但我真的卡住了,无法继续)
下面是程序片段。
更新:我已经按照 Nicol 和 ildjarn 的建议修改了程序,但问题仍然存在。
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
template <typename Iterator>
bool parse_data(Iterator first, Iterator last, std::vector< std::pair<std::string, std::string> >& v)
{
using qi::double_;
using qi::char_;
using qi::phrase_parse;
using qi::_1;
using ascii::space_type;
using phoenix::ref;
using phoenix::push_back;
std::pair<std::string, std::string> p;
qi::rule<Iterator, std::string()> key = '/' >> *char_("a-zA-Z0-9");
qi::rule<Iterator, std::string()> val = '/' >> *char_("a-zA-Z0-9");
qi::rule<Iterator, std::pair<std::string, std::string>()> ppair = key >> val ;
//bool r = phrase_parse(
bool r = parse(
first,
last,
(
+ppair[push_back(ref(v), _1)]
)
);
if (first != last)
return false;
return r;
}
}
int
main()
{
std::string str;
while (getline(std::cin, str))
{
if (str.empty())
break;
std::vector< std::pair<std::string, std::string> > vec_pair;
if(client::parse_data(str.begin(), str.end(), vec_pair))
{
std::cout << std::endl << "Parsing done" << std::endl;
std::cout << "Strings are " ;
std::pair<std::string, std::string> temp;
for(std::vector< std::pair<std::string, std::string> >::iterator i = vec_pair.begin(); i < vec_pair.end(); i++)
{
temp = *i;
std::cout << temp.first <<"|" << temp.second;
}
std::cout << std::endl;
}
else
{
std::cout << "Parsing Failed" << std::endl;
}
}
return 0;
}
错误:
/usr/local/include/boost_1_46_1/boost/spirit/home/qi/detail/assign_to.hpp:109: error: no matching function for call to
‘std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::pair(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
/usr/lib/gcc/x86_64-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_pair.h:88: note: candidates are:
std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
/usr/lib/gcc/x86_64-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_pair.h:84: note: std::pair<_T1, _T2>::pair() [with _T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
/usr/lib/gcc/x86_64-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_pair.h:73: note: std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::pair(const std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)
你能帮我解决这个问题吗?
谢谢