我是提振 spirit 的新手,遇到以下问题:
#include <string>
#include <vector>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
#include <boost/bind.hpp>
using namespace boost::spirit;
using namespace std;
struct MyGrammar
: qi::grammar<string::const_iterator, string(), ascii::space_type> {
MyGrammar();
void myFun(const string& s);
private:
qi::rule<string::const_iterator, string(), ascii::space_type> myRule;
};
using namespace boost::spirit;
using namespace std;
MyGrammar::MyGrammar() : MyGrammar::base_type(myRule) {
using qi::_1;
myRule = int_ [boost::bind(&MyGrammar::myFun, this, _1)]; // fails
myRule = int_ [_val = _1]; // fine
}
void MyGrammar::myFun(const string& s){
cout << "read: " << s << endl;
}
int
main(){
}
myRule
的第一个赋值出现编译错误,而第二个赋值编译正常。
在第一种情况下,编译器会输出大量我无法理解的错误消息。 最后它说:
boost_1_49_0/include/boost/bind/bind.hpp:318:9: error: no match for call to '(const boost::_mfi::mf1<void, MyGrammar, const std::basic_string<char>&>) (MyGrammar* const&, const boost::phoenix::actor<boost::spirit::argument<0> >&)'
boost_1_49_0/include/boost/bind/mem_fn_template.hpp:163:7: note: candidates are: R boost::_mfi::mf1<R, T, A1>::operator()(T*, A1) const [with R = void, T = MyGrammar, A1 = const std::basic_string<char>&]
boost_1_49_0/include/boost/bind/mem_fn_template.hpp:184:7: note: R boost::_mfi::mf1<R, T, A1>::operator()(T&, A1) const [with R = void, T = MyGrammar, A1 = const std::basic_string<char>&]
有什么想法吗? 非常感谢您的帮助!
最佳答案
您不能使用来自不同 bind
实现的占位符。目前 Boost 中有三个 bind
函数:
boost::bind
,由 取代
boost::lambda::bind
,由 取代
boost::phoenix::bind
,您应该将其用于 Boost.Spirit
boost::spirit::qi
(和boost::spirit::karma
)下的占位符与boost使用的相同: :phoenix::bind
,所以就用它吧。
哦,还有专业提示:停止你的 using namespace std;
,最好停止全局命名空间中的任何其他 using 指令。
关于c++ - boost::bind 不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10758078/