1

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;
           ^
sehe
  • 374,641
  • 47
  • 450
  • 633
Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
  • where is the code? Is there any question-posing tool that can make this more specific :/ ? – sehe Sep 19 '13 at 11:11
  • are you trying to create a container with `unique_ptr`? – Kiril Kirov Sep 19 '13 at 11:12
  • He's trying to copy a unique_ptr, as a part of a vector. Yes. @elvis: unique pointers can't be copyable, because... they wouldn't be very unique anymore – sehe Sep 19 '13 at 11:13
  • @KirilKirov yes I'm trying to create a `vector` of `unique_ptr` of `ADMessageReqRSSingle` – Elvis Dukaj Sep 19 '13 at 11:13
  • @elvis.dukaj That'd have been the code to show then – sehe Sep 19 '13 at 11:14
  • `typedef std::unique_ptr< ADMessageReqRSSingle > MsgType;` `typedef std::vector< MsgType > Cont;` `typedef Cont::const_iterator MsgCIter;` `Cont mCont`. Inside another class – Elvis Dukaj Sep 19 '13 at 11:15
  • so have I to use shared_ptr? – Elvis Dukaj Sep 19 '13 at 11:16
  • @elvis.dukaj Impossible to tell without more code. See [So can unique_ptr be used safely in stl collections](http://stackoverflow.com/questions/2876641/so-can-unique-ptr-be-used-safely-in-stl-collections) for a good start though – sehe Sep 19 '13 at 12:02

3 Answers3

1

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.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • But the code above worked. The class `ADMessageReqRSSingle` is moveable. I changed the code to add the `clone` method to the base class of `ADMessageReqRSSingle`. After that it stopped to work. – Elvis Dukaj Sep 19 '13 at 11:18
  • The "problem" is in `unique_ptr`, not in your class. The container is from `unique_ptr`, which has "`delete`"d copy constructor. Check this: http://stackoverflow.com/questions/2876641/so-can-unique-ptr-be-used-safely-in-stl-collections – Kiril Kirov Sep 19 '13 at 11:19
  • 3
    @elvis Please: "The code above worked"? There ***is no code***. See http://sscce.org/ – sehe Sep 19 '13 at 11:20
  • I know I cannot post the code. But the same code first worked. After adding the prototype design patter I had the issue – Elvis Dukaj Sep 19 '13 at 11:26
  • @elvis.dukaj How is that "the same code then"? Pro tip: don't fool yourself with victim logic. Look at the facts: the _changes_ you made introduced a compilation failure. Now, you will be able to search for the problem. – sehe Sep 19 '13 at 11:47
  • I just want to understand... isn't victimism! The fact are: First: I had a vector of `unique_ptr< MyClass >` and the code builds and works fine. `MyClass` is contructable and movable. Later I had to add the prototype design pattern. So I add to `IMyClass`, base class of `MyClass`, the method `virtual unique_ptr< IMyClass > clone() const = 0`. I also implemnt the method to `MyClass`. After that I had that issue. I just want to understand why first code compiles fine and after not... – Elvis Dukaj Sep 19 '13 at 12:37
  • Plain wrong. For most stl container operations move semantics is enough. – Alfa07 Feb 05 '14 at 01:30
1

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;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

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.

turoni
  • 1,345
  • 1
  • 18
  • 37