在我的项目中,我试图从虚拟方法指针中定义std :: function变量:
#include <functional>
class VirtualClass
{
public:
virtual void foo(int x) = 0;
};
class DerivedClass : public VirtualClass
{
public:
virtual void foo(int x) override
{
// I am foo from DerivedClass;
}
};
int main()
{
DerivedClass derivedClass;
std::function<void(VirtualClass&, int)> fooFunc = &VirtualClass::foo; //doesn't compile
fooFunc(derivedClass, 0);
return 0;
}
此代码正在Visual Studio 2017中编译,但不能在Visual Studio 2013中编译。是否可以在vs 2013中修复编译错误而无需切换到纯指针并继续使用std :: function?
更新:
错误消息如下:
错误1错误C2664:'void std :: _ Func_class <_Ret,VirtualClass&,int> :: _ Set(std :: _ Func_base <_Ret,VirtualClass&,int> *)':无法从'_Myimpl *转换参数1 '到'std :: _ Func_base <_Ret,VirtualClass&,int> *'c:\程序文件(x86)\ Microsoft Visual Studio 12.0 \ vc \ include \ function 506 1 ConsoleApplication1