boost :: bind不编译

fra*_*ank 3 c++ boost boost-bind boost-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(){
}
Run Code Online (Sandbox Code Playgroud)

第一次分配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>&]
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?非常感谢您的帮助!

Xeo*_*Xeo 5

您不能使用来自不同bind实现的占位符.bindBoost 目前有三个功能:

  • boost::bind,取代
  • boost::lambda::bind,取代
  • boost::phoenix::bind,这是你应该用于Boost.Spirit的东西

boost::spirit::qi(和boost::spirit::karma)下的占位符与使用的占位符相同boost::phoenix::bind,所以只需使用它.

哦,并且专业提示:using namespace std;在全局命名空间中停止你的,最好是任何其他using指令.