Ich möchte eine Memberfunktion als Parameter senden, aber es wird nicht kompilieren. Warum funktioniert der Code nicht? Hier, was ich geschrieben habe. Wenn ich stattdessen ein Lambda übergebe, funktioniert es.Senden Sie Mitglied Funktionszeiger als Parameter innerhalb einer anderen Elementfunktion
void global_func(std::function<void(void)>f)
{
f();
}
class goo
{
public:
goo() { }
void func1()
{
std::function<void(void)> fp = &goo::func2; // get a pointer to the function func2 . Error here or next line
global_func(fp);
}
void func2(void)
{
}
};
void main()
{
goo g1;
g1.func1();
}
Hier wird der Compiler-Ausgang (mein Programmname ist tryvector.cpp)
1>------ Build started: Project: TryVector, Configuration: Debug Win32 ------
1> TryVector.cpp
1>e:\program files\microsoft visual studio 12.0\vc\include\functional(506): error C2664: 'void std::_Func_class<_Ret,>::_Set(std::_Func_base<_Ret,> *)' : cannot convert argument 1 from '_Myimpl *' to 'std::_Func_base<_Ret,> *'
1> with
1> [
1> _Ret=void
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Fret(__thiscall goo::* const &)(void),_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=void (__thiscall goo::* const &)(void)
1> ]
1> e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Fret(__thiscall goo::* const &)(void),_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=void (__thiscall goo::* const &)(void)
1> ]
1> e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Fret,goo,,std::allocator<std::_Func_class<_Ret,>>>(_Fret (__thiscall goo::* const)(void),_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Fret,goo,,std::allocator<std::_Func_class<_Ret,>>>(_Fret (__thiscall goo::* const)(void),_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> e:\program files\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<void,goo,>(_Fret (__thiscall goo::* const)(void))' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> ]
1> e:\program files\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<void,goo,>(_Fret (__thiscall goo::* const)(void))' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> ]
1> d:\vc++ my files\tryvector\tryvector\tryvector.cpp(42) : see reference to function template instantiation 'std::function<void (void)>::function<void(__thiscall goo::*)(void)>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=void (__thiscall goo::*)(void)
1> ]
1> d:\vc++ my files\tryvector\tryvector\tryvector.cpp(42) : see reference to function template instantiation 'std::function<void (void)>::function<void(__thiscall goo::*)(void)>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=void (__thiscall goo::*)(void)
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
können Sie die Ausgabe des Compilers anhängen? – jdehesa
Haben Sie die Fehlermeldung gelesen? Normalerweise sagt es dir, was schief gelaufen ist. – user2079303
@ user2079303: Nicht in diesem Fall, besonders für einen Anfänger. –