删除函数unique_ptr

时间:2014-02-21 19:33:15

标签: c++ c++11

我根本不理解错误输出,我写了一个生成它的类。

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));
}

输出:

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)

2 个答案:

答案 0 :(得分:16)

您没有明确遵循“三条规则”(或者这几天是五条?)。由于您没有明确UserQueues复制构造函数,GCC将尝试生成= delete;的复制构造函数。当它这样做时,它将尝试复制您的std::vector<JobDeque>,但由于它包含仅移动std::unique_ptr,它将无法编译。因此,UserQueues的构造函数应该如下所示:

class UserQueues 
{

    public:
        explicit UserQueues();
        UserQueues(const UserQueues&) = delete;
        UserQueues& operator=(const UserQueues&) = delete;
        ~UserQueues() = default;

    [...]

};

防止编译器隐式生成复制构造函数。看到这个相关的问题,我问了两年了解更多信息How to declare a vector of unique_ptr's as class data member?

答案 1 :(得分:2)

出现错误的原因是因为正在复制。不允许复制唯一指针,因为在实现中删除了unique_ptr复制构造函数。如果您需要传递unique_ptr,则可以使用move或使用引用来传递。

另请参见