【问题标题】:unable to find what causes a compile time error无法找到导致编译时错误的原因
【发布时间】:2013-09-19 11:10:11
【问题描述】:

我正在使用带有 C++11 的 MinGW 4.8.0(包含在 QtCreator 5.1 中)。 问题是我得到一个编译时错误,但我找不到错误的来源。

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

Cont mCont; // Inside another clas

有没有更具体一点的诊断工具?

gcc 日志如下:

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;
           ^

【问题讨论】:

  • 代码在哪里?是否有任何提问工具可以使这一点更具体:/?
  • 你想用unique_ptr创建一个容器吗?
  • 他试图复制一个 unique_ptr,作为向量的一部分。是的。 @elvis:唯一指针不能被复制,因为......它们不再是唯一的
  • @KirilKirov 是的,我正在尝试创建 vector of unique_ptr of ADMessageReqRSSingle
  • @elvis.dukaj 那本来是要显示的代码

标签: c++11 mingw diagnostics


【解决方案1】:

基于错误和您对我的评论的回答:
您不能拥有带有unique_ptr 的容器,因为它不是可复制构造的,这对于所有 STL 容器都是必需的。

【讨论】:

  • 但是上面的代码有效。 ADMessageReqRSSingle 类是可移动的。我更改了代码,将clone 方法添加到ADMessageReqRSSingle 的基类中。之后它停止工作。
  • “问题”在unique_ptr,不在你的班级。容器来自unique_ptr,它具有“delete”d 复制构造函数。检查这个:stackoverflow.com/questions/2876641/…
  • @elvis 请:“上面的代码有效”? 没有代码。见sscce.org
  • 我知道我无法发布代码。但是相同的代码首先起作用。添加原型设计模式后,我遇到了问题
  • @elvis.dukaj 那“相同的代码”怎么样?专业提示:不要用受害者的逻辑欺骗自己。查看事实:您所做的更改 导致编译失败。现在,您将能够搜索问题。
【解决方案2】:

你有类似的东西:

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

所以,A 不可复制(因为unique_ptr 不可复制), 为了获得更好的错误信息,您可以明确添加

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

以下代码显示您的错误消息。

int main() {
    A a, b;

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

如果合适,您可以使用move 而不是复制。

int main() {
    A a, b;

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

编辑:只需在另一个答案的评论中查看您对克隆的评论:

克隆是一个副本(而 A 是不可复制的,因此是错误的)。

你可以使用(如果合适的话)类似的东西:

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

【讨论】:

    【解决方案3】:

    向量中的项目必须是可分配的(或者,在标准的更新版本中,是可移动的)。

    您的编译器似乎不够新,无法接受可移动部分。 如果您创建了一个同样不可分配的 const 对象向量,任何人都可能会收到此错误的另一个可能原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 2022-01-08
      • 2021-08-14
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多