【问题标题】:binding to boost::function class member compilation error绑定到 boost::function 类成员编译错误
【发布时间】:2014-05-30 20:36:39
【问题描述】:
boost::function<void()> test_func;

struct test_t
{
   boost::function<void(int)> foo_;

   void test()
   {
      // This works as expected
      test_func = boost::bind(test_t::foo_, 1);
   }

};

int main(int argc, _TCHAR* argv[])
{
      test_t test;

      test.test();

      // error C2597: illegal reference to non-static member 'test_t::foo_'
      test_func = boost::bind(test_t::foo_, &test, 1);

      const auto a = 0;

   return 0;
}

代码有什么问题?为什么代码 test_func = boost::bind(test_t::foo_, &test, 1);在 test_t::test() 中编译并在 main() 中给出错误?

谢谢

【问题讨论】:

标签: c++ boost-bind boost-function


【解决方案1】:

这是因为在方法test_t::foo_ 内部引用this-&gt;foo_,而main 中的test_t::foo_ 只能引用foo_,前提是它是该类的静态成员。你需要在那里写test.foo_

【讨论】:

    【解决方案2】:

    问题或多或少是错误所说的。 test_t::foo_ 不是函数;它是一个仿函数(函数对象),它不是静态的。因此,如果没有 test_t 对象,您将无法访问它。试试这个:

    test_func = boost::bind(test.foo_, 1);

    【讨论】:

      猜你喜欢
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      相关资源
      最近更新 更多