Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleted function unique_ptr

Tags:

c++

c++11

I don't understand the error output at all, I wrote a single class that generates it.

UserQueues.h

#ifndef USERQUEUES_H #define USERQUEUES_H  #include <deque> #include <vector> #include <memory> #include "Job.h"  class UserQueues  {     public:         typedef std::unique_ptr<Job> JobPtr;         typedef std::deque<JobPtr> JobDeque;      public:         UserQueues();         void printDeques();         void addToDeque(JobPtr job, int queueId);      public:         const uint QUEUE_QTY = 3;      private:         std::vector<JobDeque> _jobDeques; };  #endif 

UserQueues.cpp

#include <iostream> #include "UserQueues.h"  UserQueues::UserQueues() :      _jobDeques() {     for(unsigned int idx = 0 ; idx < QUEUE_QTY ; ++idx)     {         _jobDeques.push_back(JobDeque());     } }  void UserQueues::printDeques() {     for (std::size_t idx = 0; idx < _jobDeques.size(); ++idx)     {         std::cout << "[";          for(std::size_t jdx = 0 ; jdx < _jobDeques[idx].size() ; ++jdx)         {             std::cout << _jobDeques[idx].at(jdx)->getArrivalTime();              if(jdx != (_jobDeques[idx].size() - 1))             {                 std::cout << ", ";             }         }         std::cout << "]" << std::endl;     }  }  void UserQueues::addToDeque(JobPtr job, int queueId) {     _jobDeques[0].push_front(std::move(job)); } 

Output :

09:31:34 **** Incremental Build of configuration Debug for project etsmtl-log710-lab02 **** make all  make: Warning: File `src/Job.d' has modification time 1.7e+04 s in the future Building file: ../src/Job.cpp Invoking: GCC C++ Compiler g++ -std=c++0x -I"/home/patrickz/git/etsmtl-log710-lab02/include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Job.d" -MT"src/Job.d" -o "src/Job.o" "../src/Job.cpp" Finished building: ../src/Job.cpp  Building file: ../src/UserQueues.cpp Invoking: GCC C++ Compiler g++ -std=c++0x -I"/home/patrickz/git/etsmtl-log710-lab02/include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/UserQueues.d" -MT"src/UserQueues.d" -o "src/UserQueues.o" "../src/UserQueues.cpp" In file included from /usr/include/c++/4.7/deque:63:0,                  from /home/patrickz/git/etsmtl-log710-lab02/include/UserQueues.h:4,                  from ../src/UserQueues.cpp:2: /usr/include/c++/4.7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<Job>; _Args = {const std::unique_ptr<Job, std::default_delete<Job> >&}]’: /usr/include/c++/4.7/bits/stl_uninitialized.h:77:3:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<Job>, const std::unique_ptr<Job>&, const std::unique_ptr<Job>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<Job>, std::unique_ptr<Job>&, std::unique_ptr<Job>*>; bool _TrivialValueTypes = false]’ /usr/include/c++/4.7/bits/stl_uninitialized.h:119:41:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<Job>, const std::unique_ptr<Job>&, const std::unique_ptr<Job>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<Job>, std::unique_ptr<Job>&, std::unique_ptr<Job>*>]’ /usr/include/c++/4.7/bits/stl_uninitialized.h:260:63:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<Job>, const std::unique_ptr<Job>&, const std::unique_ptr<Job>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<Job>, std::unique_ptr<Job>&, std::unique_ptr<Job>*>; _Tp = std::unique_ptr<Job>]’ /usr/include/c++/4.7/bits/stl_deque.h:841:9:   required from ‘std::deque<_Tp, _Alloc>::deque(const std::deque<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<Job>; _Alloc = std::allocator<std::unique_ptr<Job> >; std::deque<_Tp, _Alloc> = std::deque<std::unique_ptr<Job> >]’ /usr/include/c++/4.7/bits/stl_construct.h:77:7:   required from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::deque<std::unique_ptr<Job> >; _Args = {std::deque<std::unique_ptr<Job, std::default_delete<Job> >, std::allocator<std::unique_ptr<Job, std::default_delete<Job> > > >&}]’ /usr/include/c++/4.7/bits/stl_uninitialized.h:77:3:   [ skipping 2 instantiation contexts ] /usr/include/c++/4.7/bits/stl_uninitialized.h:260:63:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::deque<std::unique_ptr<Job> >*; _ForwardIterator = std::deque<std::unique_ptr<Job> >*; _Tp = std::deque<std::unique_ptr<Job> >]’ /usr/include/c++/4.7/bits/stl_uninitialized.h:283:69:   required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = std::deque<std::unique_ptr<Job> >*; _ForwardIterator = std::deque<std::unique_ptr<Job> >*; _Allocator = std::allocator<std::deque<std::unique_ptr<Job> > >]’ /usr/include/c++/4.7/bits/vector.tcc:410:6:   required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {std::deque<std::unique_ptr<Job, std::default_delete<Job> >, std::allocator<std::unique_ptr<Job, std::default_delete<Job> > > >}; _Tp = std::deque<std::unique_ptr<Job> >; _Alloc = std::allocator<std::deque<std::unique_ptr<Job> > >]’ /usr/include/c++/4.7/bits/vector.tcc:102:4:   required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::deque<std::unique_ptr<Job, std::default_delete<Job> >, std::allocator<std::unique_ptr<Job, std::default_delete<Job> > > >}; _Tp = std::deque<std::unique_ptr<Job> >; _Alloc = std::allocator<std::deque<std::unique_ptr<Job> > >]’ /usr/include/c++/4.7/bits/stl_vector.h:900:9:   required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::deque<std::unique_ptr<Job> >; _Alloc = std::allocator<std::deque<std::unique_ptr<Job> > >; std::vector<_Tp, _Alloc>::value_type = std::deque<std::unique_ptr<Job> >]’ ../src/UserQueues.cpp:9:40:   required from here /usr/include/c++/4.7/bits/stl_construct.h:77:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Job; _Dp = std::default_delete<Job>; std::unique_ptr<_Tp, _Dp> = std::unique_ptr<Job>]’ In file included from /usr/include/c++/4.7/memory:86:0,                  from /home/patrickz/git/etsmtl-log710-lab02/include/UserQueues.h:6,                  from ../src/UserQueues.cpp:2: /usr/include/c++/4.7/bits/unique_ptr.h:262:7: error: declared here make: *** [src/UserQueues.o] Error 1  09:31:36 Build Finished (took 2s.52ms) 
like image 647
Patrick.SE Avatar asked Feb 21 '14 19:02

Patrick.SE


People also ask

Does unique_ptr call Delete?

unique_ptr objects automatically delete the object they manage (using a deleter) as soon as they themselves are destroyed, or as soon as their value changes either by an assignment operation or by an explicit call to unique_ptr::reset.

Do you need to delete a unique_ptr?

An explicit delete for a unique_ptr would be reset() . But do remember that unique_ptr are there so that you don't have to manage directly the memory they hold. That is, you should know that a unique_ptr will safely delete its underlying raw pointer once it goes out of scope.

What happens when unique_ptr goes out of scope?

unique_ptr. An​ unique_ptr has exclusive ownership of the object it points to and ​will destroy the object when the pointer goes out of scope.

What does unique_ptr mean in C++?

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.


1 Answers

You aren't explicitly following the "rule of three" (or is it five these days?). GCC will attempt to generate a copy constructor of UserQueues since you did not explicitly = delete; the copy constructor. When it does that, it will attempt to copy your std::vector<JobDeque>, but since that contains move-only std::unique_ptr's, it will fail to compile. So, the constructors of your UserQueues should look like:

class UserQueues  {      public:         explicit UserQueues();         UserQueues(const UserQueues&) = delete;         UserQueues& operator=(const UserQueues&) = delete;         ~UserQueues() = default;      [...]  }; 

To prevent the copy constructors from getting implicitly generated by the compiler. See this related question I asked two years for more info How to declare a vector of unique_ptr's as class data member?

like image 75
Bret Kuhns Avatar answered Oct 03 '22 21:10

Bret Kuhns