【问题标题】:boost bind compilation errorboost绑定编译错误
【发布时间】:2010-12-15 11:59:05
【问题描述】:
class A
{
   bool OutofRange(string& a, string& b, string c);
   void Get(vector <string>& str, string& a, string& b);
}

void A::Get(vector <string>& str, string& a, string& b)
{
   str.erase(
            std::remove_if (str.begin(), str.end(), BOOST_BIND(&A::OutOfRange, a, b, _1)),
            str.end()
            );
}

我收到如下错误:

 Error 7 error C2825: 'F': must be a class or namespace when followed by '::' File:bind.hpp
 Error 8 error C2039: 'result_type' : is not a member of '`global namespace'' t:\3rdparty\cpp\boost\boost-1.38.0\include\boost\bind.hpp 67 

谁能告诉我我做错了什么?

【问题讨论】:

标签: c++ boost boost-bind erase-remove-idiom


【解决方案1】:

A::OutOfRange 是一个有 4 个参数的函数 - 隐含的 *this 是第一个参数,您的 bind 子句中缺少该参数

【讨论】:

  • 我认为我没有遗漏绑定子句中的任何参数。我在这里根据这个答案编写了我的代码-stackoverflow.com/questions/1677211/sort-using-boostbind/…我的代码在这些函数不是类成员时有效,但是当我让它们成为类成员时,它们不再工作了!
  • 听走秀,他是对的。你需要这样的东西: BOOST_BIND(&A::OutOfRange, *this, a, b, _1) boost.org/doc/libs/1_40_0/libs/bind/…
  • @McBeth:事实上它更好:boost::bind( &amp;A::OutOfRange, this, a, b, _1 )。 Bind 正确处理那里的指针,另一方面,它会复制参数。在传递指针的情况下,指针被复制,如果你传递一个真实的对象,另一个对象将被创建,副本将在生成的仿函数中使用。
  • @varun:绑定指向函数的指针和指向成员函数的指针是完全不同的。成员函数有一个由编译器添加的隐式参数 Type * this(或 Type const * this,对于 const 成员函数)。当你想绑定一个成员函数时,你需要提供你希望成员方法被执行的对象。这也不同于类中的静态函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多