Reputation: 33579
I have a small template class with a non-static member of type boost::shared_mutex
. Whenever I try to compile it, I get the error:
'boost::shared_mutex::shared_mutex' : cannot access private member declared in class 'boost::shared_mutex'
.
boost::shared_mutex
really has a private nested class shared_mutex
, but I don't understand why this problem arose.
Here's my class:
#include <boost/thread.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <queue>
template <typename T>
class CThreadSafeQueue
{
public:
CThreadSafeQueue();
private:
boost::mutex _sharedMutex;
std::queue<T> _queue;
};
template <typename T>
CThreadSafeQueue<T>::CThreadSafeQueue()
{
}
The same happens with a regular `boost::mutex'.
I have another, non-template class, in which I have no problem using either mutex type.
Upvotes: 1
Views: 2677
Reputation: 33579
Huh! The solution to my problems turned out to be very simple yet very hard to find. I was only having problems with methods declared const
, because lockers are mutating mutexes. All I had to do was make it mutable
.
Upvotes: -1
Reputation: 7705
You need to make the class noncopyable, or implement your own copy and assignment operator. boost::mutex
is non copyable, therefore you get this error.
You can derive your class from boost::noncopyable
, to make it noncopyable.
Upvotes: 3