Ich versuche den folgenden Code aus here zu kompilieren, aber ich bekomme einen Kompilierfehler. Hat jemand irgendwelche Ideen, was könnte falsch sein?Std :: Funktion kompiliert nicht in VS2012
Der Code
#include <iostream>
#include <functional>
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
int main()
{
// store a call to a member function
std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
Foo foo(314159);
f_add_display(foo, 1);
}
Der Compiler-Fehler:
Error 1 error C2664: 'std::_Func_class<_Ret,_V0_t,_V1_t>::_Set' :
cannot convert parameter 1 from '_Myimpl *' to 'std::_Func_base<_Rx,_V0_t,_V1_t> *'
Dank.
@jogojapan: 'print_add' zwei Argumente übernimmt, hat es eine _implicit_' this' Argument vom Typ 'Foo const *'. –
@ K-ballo Richtig, sorry. – jogojapan
@ K-ballo: 'std :: function' verwendet automatisch' std :: mem_fn', wenn der erste Parametertyp ein (Referenz/Zeiger auf) Klassentyp des Members ist. – Xeo