36

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)
Patrick.SE
  • 4,444
  • 5
  • 34
  • 44
  • 1
    This doesn't answer your exact question, but why don't you just direct initialize `_jobDeques` like so: `UserQueues::UserQueues() : _jobDeques(QUEUE_QTY)` rather than using a loop? – user786653 Feb 21 '14 at 19:53
  • That actually fixed my problem. Why can't I use a regular loop to do it is beyond me though. – Patrick.SE Feb 21 '14 at 20:25

2 Answers2

33

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?

Bret Kuhns
  • 4,034
  • 5
  • 31
  • 43
  • Should adding these fix my problem? I just tried and I get the same error log. – Patrick.SE Feb 21 '14 at 20:04
  • Sorry, I actually threw this into gcc and you're right. This is starting to feel like either a bug in `std::deque` implemented by gcc, or it's being used improperly in your code. Unfortunately I have don't have time right now to look further. I tried using a list of deque's, and using `emplace_back` to add a new `JobQueue` to the list. It was still calling the copy constructor on `unique_ptr`. That doesn't sound correct... – Bret Kuhns Feb 21 '14 at 20:18
  • It's working now. I added your code along with user786653 suggestion. It seems that the problem comes from doing the push_back in the constructor. Although I do not know how it is done internally when using the initializer list instead... – Patrick.SE Feb 21 '14 at 20:32
  • 2
    @Pat [This might be relevant](http://stackoverflow.com/questions/19826376/insert-into-vector-having-objects-without-copy-constructor). Seems you need to provide at least one of move/copy constructors. – François Moisan Feb 21 '14 at 20:55
  • @FrançoisMoisan yes, I forgot to mention that. I was going to try `= default;`ing the move ctor/assign in `UserQueues`, but didn't have time earlier. – Bret Kuhns Feb 21 '14 at 21:56
  • 2
    I had the same problem on VS2013 with push_back in a copy constructor. Adding `push_back(std::move(...))` worked. Thanks! – TankorSmash Aug 08 '19 at 01:34
15

The reason you are getting the error is because a copy is taking place. Copying a unique pointer is not allowed because the unique_ptr copy constructor is deleted in the implementation. If you need to pass a unique_ptr, you can do so by using move or by using a reference.

See also

Aaron Swan
  • 1,206
  • 1
  • 13
  • 20