[英]c++ mutex member of class causing compilation Error
I'm not sure why this is happening when I add a mutex member to myClass(in this example mu):当我向 myClass(在本例中为 mu)添加互斥锁成员时,我不确定为什么会发生这种情况:
Error C2661 "'std::tuple<
void (__thiscall MyNameSpace::myClass::* )(void),MyNameSpace::myClass>::tuple': no overloaded function takes 2 arguments include\memory 2438
namespace MyNameSpace{
class myClass{
shared_ptr<myClass2> property;
mutex mu;
public:
myClass(shared_ptr<myClass2> arg):property(std::move(arg)) {
}
void Run(){
...........
}
}
class myClass2{
public:
myClass2(std::string str) {
trace(str);
}
}
}
int main(){
shared_ptr<myClass2> new_obj(new myClass2("somthing"));
thread(&myClass::Run, myClass(new_obj)).join();
.......other stuff.....
}
and How can i overcome this?我怎样才能克服这个?
It is a compilation error, not a memory error.这是编译错误,而不是内存错误。
Note that std::mutex
is not copiable.请注意, std::mutex
不可复制。 So, the class containing it becomes non-copiable as well.因此,包含它的类也变得不可复制。
I guess (as you do not show the relevant code) that you attempt to copy an instance of myClass
, and that causes the above compilation error.我猜(因为您没有显示相关代码)您试图复制myClass
的实例,这会导致上述编译错误。
In particular, you can search places in your code, where you pass or return myClass
instances by value.特别是,您可以在代码中搜索按值传递或返回myClass
实例的位置。
UPDATE : As pointed out by @NathanOliver, such a place in the above snippet is:更新:正如@NathanOliver 所指出的,上面代码段中的这样一个地方是:
thread(&myClass::Run, myClass(new_obj))
where a temporary myClass
is created.创建临时myClass
位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.