gpt4 book ai didi

c++ - 如何在 Visual C++ 2013 中将函数对象作为参数传递?

转载 作者:太空宇宙 更新时间:2023-11-04 15:36:39 25 4
gpt4 key购买 nike

我正在尝试将 function<> 对象作为另一个方法的参数传递,以便灵活地从列表中获取元素。这是我的代码:

libusb_device * UsbKeyboard::GetSpecifiedDevice(function<bool(libusb_device *)> pred)
{
if (_usbDevices == nullptr) return nullptr;
int i = 0;
libusb_device *dev = nullptr;
while ((dev = _usbDevices[i++]) != NULL)
{
if (pred(dev))
return dev;
}

return nullptr;
}


libusb_device * UsbKeyboard::GetHidDevice()
{
function<libusb_device *> pred = [&](libusb_device *dev) -> bool {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r >= 0)
return desc.bDeviceClass == LIBUSB_CLASS_HID;
};

return GetSpecifiedDevice(pred);
}

编译错误在这段代码的第一行:

     error C2027: use of undefined type 'std::_Get_function_impl<_Fty>'
1> with
1> [
1> _Fty=libusb_device *
1> ]
1> usbkeyboard.cpp(111) : see reference to class template instantiation 'std::function<libusb_device *>' being compiled
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(551): error C2504: 'type' : base class undefined
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(554): error C2027: use of undefined type 'std::_Get_function_impl<_Fty>'
1> with
1> [
1> _Fty=libusb_device *
1> ]
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(554): error C2146: syntax error : missing ';' before identifier '_Mybase'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(554): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

它表示函数<>的基类未定义。为什么?

最佳答案

您在 function<libusb_device *> pred 中输入错误

应该是

function<bool(libusb_device *)> pred

或简单地 auto pred

关于c++ - 如何在 Visual C++ 2013 中将函数对象作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31785210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com