Question

I'm using MinGW 4.8.0 (included in QtCreator 5.1) with C++11. The problem is that I get a compile time error but I can not find the source of the error.

typedef std::unique_ptr< ADMessageReqRSSingle > MsgType; 
typedef std::vector< MsgType > Cont; 
typedef Cont::const_iterator MsgCIter; 

Cont mCont; // Inside another clas

Is there a diagnostic tool that is a little more specific?

The gcc log is the following:

Makefile.Debug:1491: recipe for target 'debug/adsync.o' failed
In file included from c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\memory:64:0,
                     from ..\aams/iocontroller/iocontroller.hpp:15,
                     from ..\aams/aams/aamscontext.h:13,
                     from ..\aams\src\aams\adsync.cpp:8:
    c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<aams::device::ADMessageReqRSSingle>; _Args = {const std::unique_ptr<aams::device::ADMessageReqRSSingle, std::default_delete<aams::device::ADMessageReqRSSingle> >&}]':
    c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_uninitialized.h:75:53:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<aams::device::ADMessageReqRSSingle>*, std::vector<std::unique_ptr<aams::device::ADMessageReqRSSingle> > >; _ForwardIterator = std::unique_ptr<aams::device::ADMessageReqRSSingle>*; bool _TrivialValueTypes = false]'
    c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_uninitialized.h:117:41:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<aams::device::ADMessageReqRSSingle>*, std::vector<std::unique_ptr<aams::device::ADMessageReqRSSingle> > >; _ForwardIterator = std::unique_ptr<aams::device::ADMessageReqRSSingle>*]'
    c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_uninitialized.h:258:63:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<aams::device::ADMessageReqRSSingle>*, std::vector<std::unique_ptr<aams::device::ADMessageReqRSSingle> > >; _ForwardIterator = std::unique_ptr<aams::device::ADMessageReqRSSingle>*; _Tp = std::unique_ptr<aams::device::ADMessageReqRSSingle>]'
    c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_vector.h:316:32:   required from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<aams::device::ADMessageReqRSSingle>; _Alloc = std::allocator<std::unique_ptr<aams::device::ADMessageReqRSSingle> >]'
    ..\aams/aams/device/admessagereq.h:687:50:   required from here
    c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_construct.h:75:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = aams::device::ADMessageReqRSSingle; _Dp = std::default_delete<aams::device::ADMessageReqRSSingle>]'
         { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
           ^
    In file included from c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\memory:81:0,
                     from ..\aams/iocontroller/iocontroller.hpp:15,
                     from ..\aams/aams/aamscontext.h:13,
                     from ..\aams\src\aams\adsync.cpp:8:
    c:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\unique_ptr.h:273:7: error: declared here
           unique_ptr(const unique_ptr&) = delete;
           ^
Was it helpful?

Solution

Based on the errors and on your answer to my comment:
You can't have container with unique_ptr as it's not copy-constructable, which is necessary for all STL containers.

OTHER TIPS

You have something like:

class A
{
public:
    A() {}
private:
    std::vector<std::unique_ptr<int>> ints;
};

So, A is not copyable (because unique_ptr is not), To have better error message, you may add explicitly

    A(const A&) = delete;
    A& operator = (const A&) = delete;

Following code shows your error message.

int main() {
    A a, b;

    a = b; // Error appears due to this copy 
    return 0;
}

You may use move instead of copy if appropriate.

int main() {
    A a, b;

    a = std::move(b); // No error, but b is now "invalid"
    return 0;
}

EDIT: Just see your comment about Clone in comment in another answer:

Clone is a copy (and A is not copyable so an error).

you may use (if appropriate) something like:

std::unique_ptr<A> A::Clone() const {
    std::unique_ptr<A> res(new A());
    for (auto i : ints) {
        // copy the value, not the pointer.
        res->ints.push_back(std::unique_ptr<int>(new int(*i)));
    }
    return res;
}

Items in a vector must be assignable (or, in more recent versions of the standard, movable).

You compiler does not seem to be recent enough to accept the moveable part. Another possible reason anyone might get this error if you made a vector of const objects which are also not assignable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top