gpt4 book ai didi

c++ - 错误 : invalid use of non-static member function ‘int test::hotplug_callback(libusb_context*, libusb_device*, libusb_hotplug_event, void*)’

转载 作者:行者123 更新时间:2023-11-28 01:32:10 33 4
gpt4 key购买 nike

我从

改编了 libusb 热插拔示例

* libusb example program for hotplug API
* Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.com>

(下面的代码)作为测试并将其放在一个类中,因为我的真实程序也有同样的问题。我知道如果我将 static 放在它起作用的两个回调前面,但我不希望它们是静态的。我想从回调中访问我的实例变量。这个想法是用户插入一个 usb 设备,我们称它为 usbXYZ。从回调中,我实例化类 usbXYZ 并放入一个 std::map - 用户将其删除,然后我将其从 map 中删除。包含 std::map 及其中的对象的类有一个“更高级别”的方法来写入设备。

请问如何使回调函数工作非静态?如果可能的话,请解释一下,因为我不明白。谢谢。

#include <stdlib.h>
#include <stdio.h>
#include <thread>

#include "libusb-1.0/libusb.h"

class test{
public:
test() {
okGo();
}

private:
int done = 0;
libusb_device_handle *handle = NULL;

int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
struct libusb_device_descriptor desc;
int rc;

(void)ctx;
(void)dev;
(void)event;
(void)user_data;

rc = libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error getting device descriptor\n");
}

printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);

if (handle) {
libusb_close (handle);
handle = NULL;
}

rc = libusb_open (dev, &handle);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error opening device\n");
}

done++;

return 0;
}

int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
(void)ctx;
(void)dev;
(void)event;
(void)user_data;

printf ("Device detached\n");

if (handle) {
libusb_close (handle);
handle = NULL;
}

done++;

return 0;
}

int okGo(){
libusb_hotplug_callback_handle hp[2];
int product_id, vendor_id, class_id;
int rc;

vendor_id = LIBUSB_HOTPLUG_MATCH_ANY;
product_id = LIBUSB_HOTPLUG_MATCH_ANY;
class_id = LIBUSB_HOTPLUG_MATCH_ANY;

rc = libusb_init (NULL);
if (rc < 0)
{
printf("failed to initialise libusb: %s\n", libusb_error_name(rc));
return EXIT_FAILURE;
}

if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
printf ("Hotplug capabilites are not supported on this platform\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}

rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_ENUMERATE, vendor_id,
product_id, class_id, hotplug_callback, NULL, &hp[0]);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error registering callback 0\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}

rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_ENUMERATE, vendor_id,
product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error registering callback 1\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}

while (done < 20) {
//rc = libusb_handle_events (NULL);
if (libusb_handle_events_completed(nullptr, nullptr) != LIBUSB_SUCCESS)
printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
}

if (handle) {
libusb_close (handle);
}

libusb_exit (NULL);

return EXIT_SUCCESS;
}
};


int main(int argc, char *argv[])
{
std::unique_ptr<test> testClass1 = std::make_unique<test>();

//just test
for (;;)
{
//main service loop
std::this_thread::sleep_for(std::chrono::microseconds(2000000));
}
}

最佳答案

libusb 是一个C 库,它只能接受非成员函数指针。非成员函数(或 static 成员函数)指针与指向非静态成员函数的指针之间的区别在于,非静态成员函数需要一个对象 被调用,而 C 对 C++“对象”一无所知。

但是使用 libusb 可以解决它,使用 static 成员函数和用户数据指针(您现在传递一个空指针)。

例如

class test
{
// ...

static int LIBUSB_CALL static_hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
return reinterpret_cast<test*>(user_data)->hotplug_callback_detach(ctx, dev, event);
}

int hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event)
{
// Current code
}

// ...

int okGo()
{
// ...

rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_ENUMERATE, vendor_id,
product_id,class_id, static_hotplug_callback_detach, this, &hp[1]);

// ...
}

请注意,在注册回调时使用了 static 成员函数,并且 this 作为用户数据指针传递。

关于c++ - 错误 : invalid use of non-static member function ‘int test::hotplug_callback(libusb_context*, libusb_device*, libusb_hotplug_event, void*)’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51050629/

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