C ++ std :: thread“尝试使用已删除的函数” [英] C++ std::thread "Attempt to use a deleted function"

查看:3628
本文介绍了C ++ std :: thread“尝试使用已删除的函数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌和这里找一段时间,我找不到任何合适的解决方案,所以忍受我,如果我错过了。



,这里是相关的代码和相关的错误,我不知道该怎么做。

  Breaker :: Thread :: Thread(std :: string name,std :: string desc,void * func)
{
std :: thread _thread(func);

_thread.join();
}

这是在thread.cpp,接下来是在log.cpp ...

  thread = new Breaker :: Thread(System Log,loop); 

  void * Breaker :: Log :: loop()
{
add(test);
}

以下是相关错误:



/ b / b / b / b / b / b / b / b / b文件夹中的文件/ b / block / nope / Documents / dev / C ++ / Breaker Engine / src / core / thread.h:28:
/usr/bin/../include/c++/v1/thread:332:5:error:试图使用删除的函数
__invoke(_VSTD :: move(_VSTD :: get< 0>(__ t)),_VSTD :: move(_VSTD :: get< _Indices>(__ t))...)
^
/usr/bin/../include/c++/v1/thread:342:5:注意:在实例化函数模板专业化'std :: __ 1 :: __ thread_execute'请求这里
__thread_execute(* __ p,_Index());
^
/usr/bin/../include/c++/v1/thread:354:42:注意:在实例化函数模板专门化'std :: __ 1 :: __ thread_proxy>'
请求这里
int __ec = pthread_create(& __ t_,0,& __ thread_proxy <_Gp>,__p.get());
^
/ home / nope / Documents / dev / C ++ / Breaker Engine / src / core / thread.cpp:95:14:注意:在实例化函数模板专用化时
'std: :__ 1 :: thread :: thread'request here
std :: thread _thread(func);
^
/usr/bin/../include/c++/v1/type_traits:1027:5:注意:'〜__nat'已被明确标记为已删除
〜__nat()=删除;
^



解决方案

我认为问题是参数< c $ c> func 。它被声明为一个void指针,而不是一个返回void的函数的指针。



而不是

  Breaker :: Thread :: Thread(std :: string name,std :: string desc,void * func)

我想你的意思是,

  Breaker :: Thread :: Thread :: string name,std :: string desc,void(* func)())

loop 必须是静态成员才能正常工作。



std :: function。这是一个比使用void指针更现代和更干净的界面。

  Breaker :: Thread :: Thread(std :: string name,std :: string desc,std :: function   


I've been looking on google and here for a while and I can't find any suitable solutions, so bear with me if I missed it.

Anyways, here's the relevant code and the relevant error, I'm not really sure what to make of it.

Breaker::Thread::Thread(std::string name, std::string desc, void* func)
{
    std::thread _thread(func);

    _thread.join();
}

That's in thread.cpp, the next is in log.cpp...

thread = new Breaker::Thread("System Log", loop);

and

void* Breaker::Log::loop()
{
    add("test");
}

Here's the relevant error:

In file included from /home/nope/Documents/dev/C++/Breaker Engine/src/core/thread.cpp:25: In file included from /home/nope/Documents/dev/C++/Breaker Engine/src/core/thread.h:28: /usr/bin/../include/c++/v1/thread:332:5: error: attempt to use a deleted function __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); ^ /usr/bin/../include/c++/v1/thread:342:5: note: in instantiation of function template specialization 'std::__1::__thread_execute' requested here __thread_execute(*__p, _Index()); ^ /usr/bin/../include/c++/v1/thread:354:42: note: in instantiation of function template specialization 'std::__1::__thread_proxy >' requested here int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get()); ^ /home/nope/Documents/dev/C++/Breaker Engine/src/core/thread.cpp:95:14: note: in instantiation of function template specialization 'std::__1::thread::thread' requested here std::thread _thread(func); ^ /usr/bin/../include/c++/v1/type_traits:1027:5: note: '~__nat' has been explicitly marked deleted here ~__nat() = delete; ^

解决方案

I think the problem is the declaration of the parameter func. It is declared as a void pointer instead of a pointer to a function returning void.

Instead of,

Breaker::Thread::Thread(std::string name, std::string desc, void* func)

I think you mean,

Breaker::Thread::Thread(std::string name, std::string desc, void (*func) ())

Note, loop must be a static member for this to work.

Additionally, you might want to consider using std::function. It's a much more modern and cleaner interface than using void pointers.

Breaker::Thread::Thread(std::string name, std::string desc, std::function<void()> func)

这篇关于C ++ std :: thread“尝试使用已删除的函数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆