gpt4 book ai didi

对于参数 ‘sender’ 到 ‘void*’,C++ 无法将 ‘1’ 转换为 ‘void* sending(void*)’

转载 作者:行者123 更新时间:2023-11-27 22:55:56 25 4
gpt4 key购买 nike

我在 sender 类中有 startSending 过程和一个友元函数 (sending)。我想从一个新线程调用 friend 函数,所以我在 startSending 过程中创建了一个新线程。

class sender{
public:
void startSending();

friend void* sending (void * callerobj);
}

void sender::startSending(){
pthread_t tSending;
pthread_create(&tSending, NULL, sending(*this), NULL);
pthread_join(tSending, NULL);
}

void* sending (void * callerobj){
}

但是我得到了这个错误

cannot convert ‘sender’ to ‘void*’ for argument ‘1’ to ‘void* sending(void*)’

从 pthread_create 调用发送的正确方法是什么?

最佳答案

pthread_create 签名如下所示:

int pthread_create(pthread_t *thread, //irrelevant
const pthread_attr_t *attr, //irrelevant
void *(*start_routine) (void *), //the pointer to the function that is going to be called
void *arg); //the sole argument that will be passed to that function

所以在你的情况下,指向发送指针必须作为第三个参数传递,而this(将传递的参数to sending) 需要作为最后一个参数传递:

pthread_create(&tSending, NULL, &sending, this);

关于对于参数 ‘sender’ 到 ‘void*’,C++ 无法将 ‘1’ 转换为 ‘void* sending(void*)’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33119831/

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