本文介绍了使用boost ::互斥体 - 隐式删除错误(因为默认定义将被虐待的形成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类看起来如 .H 如下文件(头)

I have class looks as follows in .h file (Header)

#include <boost/thread.hpp>

class MyClass{

    private:
        boost::mutex bPoolMtx_;

        // ... other vars
    public:
        // public vars and methods

}

我收到以下错误试图建立/编译。

I get the following error trying to build/ compile.

MyClass.h:38:7: note: ‘MyClass::MyClass(const MyClass&)’ is implicitly deleted because the default definition would be ill-formed:
MyClass.h:38:7: error: use of deleted function ‘boost::mutex::mutex(const boost::mutex&)’

我不cpp文件在所有使用互斥体呢。当我注释掉的boost ::互斥行它建立的罚款。这是怎么回事?

I don't use the mutex at all in the cpp file yet. When I comment out the boost::mutex line it builds fine. What is going on?

推荐答案

由编译器副本默认情况下所有数据成员生成的默认拷贝构造函数。您使用的boost ::互斥因为互斥的复制,抛出一个错误。

The default copy constructor generated by the compiler copies all data members by default. Your use of boost::mutex throws an error because the mutex isn't copyable.

您可以编写自己的拷贝构造函数,这并不试图复制互斥或者干脆删除 MyClass的拷贝构造函数。

You can write your own copy constructor that doesn't attempt to copy the mutex or simply delete the copy constructor for MyClass.

#include <boost/thread.hpp>

class MyClass{
    private:
        boost::mutex bPoolMtx_;

        // ... other vars
    public:
        // public vars and methods
        MyClass(const MyClass&) = delete;
}

这篇关于使用boost ::互斥体 - 隐式删除错误(因为默认定义将被虐待的形成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:32