c ++互斥类成员导致编译错误 [英] c++ mutex member of class causing compilation Error
问题描述
当我向 myClass(在本例中为 mu)添加互斥锁成员时,我不确定为什么会发生这种情况:
I'm not sure why this is happening when I add a mutex member to myClass(in this example 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.
注意 std::mutex
是不可复制的.因此,包含它的类也变得不可复制.
Note that std::mutex
is not copiable. So, the class containing it becomes non-copiable as well.
我猜测(因为您没有显示相关代码)您试图复制 myClass
的实例,并导致上述编译错误.
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.
更新:正如@NathanOliver 所指出的,上面代码段中的这样一个地方是:
UPDATE: As pointed out by @NathanOliver, such a place in the above snippet is:
thread(&myClass::Run, myClass(new_obj))
在其中创建临时 myClass
.
这篇关于c ++互斥类成员导致编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!