Weird problem with boost::noncopyable and boost::mutex

Hi, *I was doing a program in C++ where I simulate a Java's ArrayList...* * * template <class T> class Duo { public: Duo *next; T element; Duo(T el,Duo *sig) { element=el; next=sig; } ~Duo() {}; Duo* getNext() {return next;} T getElement() {return element;} void setNext(Duo *myNext) {next=myNext;} void setElement(T el){element=el;} }; template <class T> class ArrayList { public: Duo <T>*Ptr_First; Duo <T>*Ptr_Last; ArrayList(){Ptr_First = Ptr_Last = NULL;}; ~ArrayList(){} void insert(T el); std::pair<T, int> get(int index); }; template <class T> void ArrayList<T>::insert(T elem) { Duo<T> *ptr_walker = Ptr_First; Duo<T> *ptr_cache; if (Ptr_First == NULL && Ptr_Last == NULL) { Duo<T> *Ptr_temp = new Duo<T>(elem, NULL); Ptr_First = Ptr_temp; Ptr_Last = Ptr_temp; } else { while (ptr_walker->getNext() != NULL) ptr_walker = ptr_walker->getNext(); ptr_cache = ptr_walker; Duo<T> *Ptr_temp = new Duo<T>(elem, NULL); Ptr_Last = Ptr_temp; ptr_walker->setNext(Ptr_temp); } } template <class T> std::pair<T, int> ArrayList<T>::get(int index) { Duo<T> *ptr_walker; std::pair <T, int> receiver; receiver.first = ptr_walker->getElement(); receiver.second = 0; bool found = false; if ((Ptr_First == NULL) && (Ptr_Last == NULL)) return receiver; else { ptr_walker = Ptr_First; for (int i = 0; i<index; i++) { ptr_walker = ptr_walker->getNext(); } receiver.first = ptr_walker->getElement(); receiver.second = 1; return receiver; } } *The thing is that I instanciated an ArrayList object and called the "insert" method and I received an error from boost::mutex AND boost::noncopyable!! and I am not using them at all!!* *This is the code that generates the error:* ArrayList<ObjectA> myArrayListA; ObjectA myObjectA; myArrayListA.insert(myObjectA); *and the errors I got:* mkdir -p build/Debug/GNU-MacOSX rm -f build/Debug/GNU-MacOSX/main.o.d g++ -pthreads -lboost_thread -c -g -fPIC -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp /usr/local/include/boost/noncopyable.hpp: In copy constructor 'boost::mutex::mutex(const boost::mutex&)': In file included from main.cpp:23: /usr/local/include/boost/noncopyable.hpp:27: error: 'boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)' is private /usr/local/include/boost/thread/pthread/mutex.hpp:31: error: within this context ObjectA.h: In copy constructor 'ObjectA::ObjectA(const ObjectA&)': ObjectA.h:18: note: synthesized method 'boost::mutex::mutex(const boost::mutex&)' first required here *Any Ideas why am I receiving this errors?* THANKS!! Dann

The thing is that I instanciated an ArrayList object and called the "insert" method and I received an error from boost::mutex AND boost::noncopyable!! and I am not using them at all!! This is the code that generates the error: ArrayList<ObjectA> myArrayListA; ObjectA myObjectA; myArrayListA.insert(myObjectA);
Maybe ObjectA has a member of type boost::mutex? insert() invokes compiler-generated copy-constructor of ObjectA, which in turn tries to copy the mutex. In short, you ObjectA is non-copyable.

You were right. Actually ObjectA has a member that is ListObject that has a member boost::mutex. I really need to use this objects this way and the LisObject object needs that mutex, is any other way I can use them and not recesive this problem? Thanks!! Dann On Wed, Mar 17, 2010 at 7:00 PM, Igor R <boost.lists@gmail.com> wrote:
The thing is that I instanciated an ArrayList object and called the "insert" method and I received an error from boost::mutex AND boost::noncopyable!! and I am not using them at all!! This is the code that generates the error: ArrayList<ObjectA> myArrayListA; ObjectA myObjectA; myArrayListA.insert(myObjectA);
Maybe ObjectA has a member of type boost::mutex? insert() invokes compiler-generated copy-constructor of ObjectA, which in turn tries to copy the mutex. In short, you ObjectA is non-copyable. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Thu, Mar 18, 2010 at 2:37 PM, Daniel V <dann.vd@gmail.com> wrote:
You were right. Actually ObjectA has a member that is ListObject that has a member boost::mutex. I really need to use this objects this way and the LisObject object needs that mutex, is any other way I can use them and not recesive this problem? Thanks!!
Dann
You could use a pointer to your object, which is non-copyable and store the pointer in the list than. If you decide to go that way consider using boost pointer containers, which wrap pointer semantics and do automatic memory management of contained pointers. Here is the lib ref: http://www.boost.org/doc/libs/1_42_0/libs/ptr_container/doc/ptr_container.ht... Regards, Ovanes

You were right. Actually ObjectA has a member that is ListObject that has a member boost::mutex. I really need to use this objects this way and the LisObject object needs that mutex, is any other way I can use them and not recesive this problem? Thanks!!
You can either store [smart] pointer to the object, like Ovanes proposed, or make your ObjectA copyable by storing [smart]ptrs to non-copyable members. In the latter case you have to think how ObjectA internals should be copied in the copy-constructor and operator=().
participants (3)
-
Daniel V
-
Igor R
-
Ovanes Markarian