简体   繁体   中英

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):

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. 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.

In particular, you can search places in your code, where you pass or return myClass instances by value.

UPDATE : As pointed out by @NathanOliver, such a place in the above snippet is:

thread(&myClass::Run, myClass(new_obj))

where a temporary myClass is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM