gpt4 book ai didi

C++ std::线程 "Attempt to use a deleted function"

转载 作者:太空狗 更新时间:2023-10-29 21:41:18 26 4
gpt4 key购买 nike

这是相关代码和相关错误,我不太确定该怎么做。

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");
}

这是相关的错误:

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<void *>' 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<std::__1::tuple<void *> >'
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<void *&, void>' 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;
^

最佳答案

我认为问题出在参数func 的声明上。它被声明为 void 指针,而不是指向返回 void 的函数的指针。

代替,

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

我想你的意思是,

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

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

此外,您可能需要考虑使用 std::function。这是一个比使用 void 指针更现代、更简洁的界面。

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

关于C++ std::线程 "Attempt to use a deleted function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29191445/

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