2015-11-10 42 views
-1

這真的很奇怪。首先,我不知道你可以刪除函數,其次,這發生在外部庫中。錯誤:使用刪除函數boost :: shared_mutex :: shared_mutex

錯誤的情況是,我正在使用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: 
+0

而[你的代碼再現問題](http://stackoverflow.com/help/mcve)是...? – Angew

+0

我甚至不知道哪個代碼導致它。 –

+0

然後你還沒有做足夠的事情來自己隔離和識別問題。但編譯器錯誤的起源(「'myprogram.h:2','myprogram.cpp:1'」)可能是一個很好的起點。如果這沒有幫助,只需繼續扔掉不涉及'shared_mutex'的代碼,直到錯誤消失。然後,你會更接近查明它。 – Angew

回答

2

你試圖複製一個互斥體。這是不可能的。

你觸發,從

from myprogram.h:2, 
from myprogram.cpp:1: 

所以,這是代碼。很可能,如果代碼中沒有明確說明,則在類中有一個shared_mutex作爲成員,並且此類將作爲其餘代碼的一部分進行復制。

例如爲:

struct MyClass { 
    boost::shared_mutex sm; 
}; 

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

vector將在許多操作copy.move它的元素,而這將引發沿途的互斥副本。

有關背景:

+0

哇,你很好。我有這個類的長初始化 - 「MyClass inst = MyClass()'。 GCC可能對待這與MSVC不同,並引發錯誤。我無法想象我自己會想到這件事。 –

+0

稱之爲經驗:)乾杯。順便說一下:[複製初始化](http://en.cppreference.com/w/cpp/language/copy_initialization)(即使不使用,也需要可訪問的複製分配操作符) – sehe

0

First I didn't know that you can delete functions

C++ 11允許:

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

另一個類似的特徵被標記爲缺省值(=default)函數。關於刪除和默認功能的MSDN上的好文章:click

second, this happens in external library.

您試圖從該庫複製shared_mutex - 這是不可能的。想一想 - 「互斥體」代表什麼?

也許你有一些代碼,介紹複製 - 你在std::容器中存儲一些包含互斥對象的對象嗎?

相關問題