【问题标题】:error: use of deleted function boost::shared_mutex::shared_mutex错误:使用已删除的函数 boost::shared_mutex::shared_mutex
【发布时间】:2015-11-10 12:36:14
【问题描述】:

这真的很奇怪。首先我不知道你可以删除函数,其次,这发生在外部库中。

错误的情况是我正在使用 QtCreator 构建项目并一起提升,没有任何静态库。

使用的编译器是gcc

myprogram.h:4:7: error: use of deleted function 'boost::shared_mutex::shared_mutex(const boost::shared_mutex&)'
In file included from ../libs/boost155/boost/thread/lock_guard.hpp:11:0,
                 from ../libs/boost155/boost/thread/pthread/thread_data.hpp:11,
                 from ../libs/boost155/boost/thread/thread_only.hpp:17,
                 from ../libs/boost155/boost/thread/thread.hpp:12,
                 from ../libs/boost155/boost/thread.hpp:13,
                 from myprogram.h:2,
                 from myprogram.cpp:1:

【问题讨论】:

  • 我什至不知道是哪个代码导致的。
  • 那么您在自己隔离和识别问题方面做得还不够。但是编译器错误的来源(“myprogram.h:2,myprogram.cpp:1”)可能是一个很好的起点。如果这没有帮助,只需继续丢弃不涉及shared_mutex 的代码,直到错误消失。然后,您将更接近精确定位。

标签: c++ gcc boost


【解决方案1】:

您正在尝试复制互斥锁。这是不可能的。

你是从

触发的
 from myprogram.h:2,
 from myprogram.cpp:1:

那么,这就是你的代码。可能,如果它在您的代码中不明确,您有一个 shared_mutex 作为类中的成员,并且该类被复制,作为其余代码的一部分。

例如:

struct MyClass {
    boost::shared_mutex sm;
};

std::vector<MyClass> v;
// etc.

vector 将在许多操作中复制.移动其元素,这将触发互斥体复制。

背景:

【讨论】:

  • 哇,你真棒。我对该类进行了长时间的初始化-MyClass inst = MyClass()。 GCC 可能会以不同于 MSVC 的方式处理它并引发错误。我无法想象我自己会想到这一点。
  • 称之为体验 :) 干杯。顺便说一句:copy initialization(需要可访问的复制分配运算符,即使不使用)
【解决方案2】:

首先我不知道你可以删除函数

C++ 11 允许:

struct noncopyable
{
  noncopyable(const noncopyable&) =delete;
  noncopyable& operator=(const noncopyable&) =delete;
};

另一个类似的功能是标记为默认的功能 (=default)。 MSDN 上关于删除和默认功能的好文章:click

其次,这发生在外部库中。

您尝试从该库中复制shared_mutex - 这是不可能的。想一想——“互斥副本”代表什么?

您可能有一些代码,引入了复制 - 您是否将一些包含互斥锁的对象存储在 std:: 容器中?

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2014-02-02
    • 2012-03-12
    • 1970-01-01
    • 2013-08-27
    相关资源
    最近更新 更多