[英]use of deleted function 'std::thread::thread(const std::thread&)'
I have class eventEngine and gateway like below: 我有类eventEngine和网关,如下所示:
class eventEngine
{
public:
eventEngine();
std::thread threa;
std::thread timer;
};
class Gateway
{
protected:
eventEngine ee;
std::string gatewayName;
};
constructor for gateway: 网关构造函数:
Gateway::Gateway(eventEngine ee, std::string gatewayName)
{
this->ee.threa = std::move(ee.threa);
this->ee.timer = std::move(ee.timer);
this->gatewayName = gatewayName;
}
and main1.cpp: 和main1.cpp:
int main()
{
eventEngine e;
std::string names = "abc";
Gateway g(e,names);
return 0;
}
when I try to compile in main1.cpp, i get error: 当我尝试在main1.cpp中编译时,我得到错误:
main1.cpp:12:21: error: use of deleted function 'eventEngine::eventEngine(const eventEngine&)'
Gateway g(e,names);
^
In file included from Gateway.h:11:0,
from main1.cpp:2:
eventEngine.h:25:7: note: 'eventEngine::eventEngine(const eventEngine&)' is implicitly deleted because the default definition would be ill-formed:
class eventEngine
^
eventEngine.h:25:7: error: use of deleted function 'std::thread::thread(const std::thread&)'
In file included from Gateway.h:8:0,
from main1.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/thread:126:5: note: declared here
thread(const thread&) = delete;
^
In file included from Gateway.h:11:0,
from main1.cpp:2:
eventEngine.h:25:7: error: use of deleted function 'std::thread::thread(const std::thread&)'
class eventEngine
I have searched similar problems, it looks like there are problems with std::thread, thread is non-copy class, I have changed to std::move as in this->ee.threa = std::move(ee.threa); 我搜索过类似的问题,看起来std :: thread存在问题,线程是非复制类,我已经改为std :: move,因为 - > ee.threa = std :: move(ee.threa ); this->ee.timer = std::move(ee.timer); this-> ee.timer = std :: move(ee.timer); but it still gives me error, what is the problem here? 但它仍然给我错误,这里有什么问题?
To make this work, you should change your code: 要使这项工作,您应该更改您的代码:
Gateway::Gateway(eventEngine&& ee, std::string&& gatewayName)
{
this->ee = std::move(ee);
this->gatewayName = std::move(gatewayName);
}
and 和
Gateway g(std::move(e), std::move(names));
or simply 或者干脆
Gateway g(eventEngine{}, "abc");
But the best way is to write in standard form: 但最好的方法是以标准形式书写:
Gateway::Gateway(eventEngine&& ee, std::string&& gatewayName) : ee{std::move(ee)}, gatewayName{std::move(gatewayName)} {}
Your code doesn't work because you try to init function argument with copy-ctor, which is deleted due to deleted std::thread
's and respectively eventEngine
's copy-ctors. 您的代码不起作用,因为您尝试使用copy-ctor初始化函数参数,该函数由于删除了std::thread
和eventEngine
的copy- eventEngine
而被删除。 You should use move-ctors instead of them. 你应该用move-ctors代替它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.