为非静态成员函数创建unary_function函数
create an unary_function functor for non-static member function
代码应该解释了我的困难。虽然代码本身毫无意义,但我计划在MyClass中添加容器,并使用带有成员函数的算法。
#include <cstdlib>
#include <algorithm>
#include <functional>
using namespace std;
class MyClass
{
public:
MyClass() { a = 0; }
~MyClass() {}
private:
int a;
bool tiny_test (int);
int Func();
};
bool MyClass::tiny_test (int b)
{
return a == b;
}
int MyClass::Func()
{
// does not compile
(mem_fun(&MyClass::tiny_test))(this);
// commented below is another attempt, also no success
//mem_fun1_t<bool, MyClass, int> tmp_functor = mem_fun(&MyClass::tiny_test);
//tmp_functor(this);
return 0;
}
int main(int argc, char** argv)
{
return 0;
}
非常感谢!顺便说一句,我没有使用静态成员函数,只是因为我相信它必须适用于非静态成员函数。P.S. Eric, Jarod42,感谢您的及时回复!
bool MyClass::tiny_test (int b)
{ // ^^^^^ You missed this argument
return a == b;
}
试试这个:
// Supply one more argument. E.g., 3
(mem_fun(&MyClass::tiny_test))(this, 3);
相关文章:
- "error: no matching function for call to"构造函数错误
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 将函数包装器转换为 std::function
- 类型擦除的std::function与虚拟函数调用的开销
- 如何为 v8::P ersistent<v8::Function> 创建到 c++ 函数的包装器
- C++复制函数重载导致"must be a nonstatic member function"错误
- C++派生类重载函数(带有 std::function 参数)不可见
- 如何将函数指针从 std::function 传递到 Linux 克隆?
- 如何在跳过所有内部实现的同时跳转到 GDB 中 std::function 中的函数?
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- C++:从重载函数转换为 std::function
- 仅当一个参数中未使用 std::function 时,模板函数替换才有效
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 插件:TypeError:addons.function() 不是一个函数
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 将另一个类的成员函数传递到 std::function 参数中
- 如何从类成员函数返回指针,例如 size_t * class :: function(); 并使用类析构函数 ~size
- 创建函数变体向量时"No matching function for call"错误
- 在windows上使用ctypes在python中包装c++函数:function未找到
- 我所有的函数都有错误 C2084:函数'function'已经有一个主体