使用泛型STD:在一個類中具有成員函數(shù)的函數(shù)對象對于一個類,我想在一個類中存儲一些指向同一個類的成員函數(shù)的函數(shù)指針。map貯存std::function物品。但我在一開始就失敗了,這段代碼是:class Foo {
public:
void doSomething() {}
void bindFunction() {
// ERROR
std::function<void(void)> f = &Foo::doSomething;
}};我收到error C2064: term does not evaluate to a function taking 0 arguments在……里面xxcallobj結(jié)合一些奇怪的模板實例化錯誤。目前,我在Windows 8上使用VisualStudio 2010/2011,在Win 7上使用VS 10,它也失敗了。錯誤必須基于一些我不遵循的奇怪的C+規(guī)則。編輯:我知道不用助推。這是集成在MS編譯器中的C+11。
3 回答

MMMHUHU
TA貢獻(xiàn)1834條經(jīng)驗 獲得超8個贊
std::function
<void(void)>
std::function<void(void)> f = std::bind(&Foo::doSomething, this);
using namespace std::placeholders;std::function<void(int,int)> f = std::bind(&Foo::doSomethingArgs, this, _1, _2);
std::function<void(int,int)> f = [=](int a, int b) { this->doSomethingArgs(a, b);}

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗 獲得超8個贊
std::function<void(Foo*)> f = &Foo::doSomething;
this
std::function<void(void)> f = std::bind(&Foo::doSomething, this);

慕虎7371278
TA貢獻(xiàn)1802條經(jīng)驗 獲得超4個贊
class MyClass{public: void MemberFunc(int value) { //do something }};// Store member function bindingauto callable = std::mem_fn(&MyClass::MemberFunc); // Call with late supplied 'this'MyClass myInst;callable(&myInst, 123);
std::_Mem_fn_wrap<void,void (__cdecl TestA::*)(int),TestA,int> callable
std::function<void(int)> binding = std::bind(callable, &testA, std::placeholders::_1);binding(123); // Call
- 3 回答
- 0 關(guān)注
- 586 瀏覽
添加回答
舉報
0/150
提交
取消