• 头文件循环引用的错误


    In file included from /usr/include/c++/5/memory:81:0,
    from Threadpool.h:12,
    from testThreadpool.cc:7:
    /usr/include/c++/5/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = wd::Thread]’:
    /usr/include/c++/5/bits/unique_ptr.h:236:17: required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = wd::Thread; _Dp = std::default_delete<wd::Thread>]’
    /usr/include/c++/5/bits/stl_construct.h:93:7: required from ‘void std::_Destroy(_Tp*) [with _Tp = std::unique_ptr<wd::Thread>]’
    /usr/include/c++/5/bits/stl_construct.h:103:19: required from ‘static void std::_Destroy_aux<<anonymous> >::__destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::unique_ptr<wd::Thread>*; bool <anonymous> = false]’
    /usr/include/c++/5/bits/stl_construct.h:127:11: required from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::unique_ptr<wd::Thread>*]’
    /usr/include/c++/5/bits/stl_construct.h:151:15: required from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>&) [with _ForwardIterator = std::unique_ptr<wd::Thread>*; _Tp = std::unique_ptr<wd::Thread>]’
    /usr/include/c++/5/bits/stl_vector.h:424:22: required from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = std::unique_ptr<wd::Thread>; _Alloc = std::allocator<std::unique_ptr<wd::Thread> >]’
    Threadpool.h:34:19: required from here
    /usr/include/c++/5/bits/unique_ptr.h:74:22: error: invalid application of ‘sizeof’ to incomplete type ‘wd::Thread’
    static_assert(sizeof(_Tp)>0,
    ^
    Makefile:12: recipe for target 'a.out' failed
    make: *** [a.out] Error 1

    最近在写代码的时候,出现一个莫名奇妙的错误,查找了一个多小时,代码功能方面没有错误,但是出现一个看不懂的额问题,终于发现错误所在,原来是头文件循环引用了!!!

    铭记:实在找不出代码问题所在,不妨查看一下头文件引用是否错误!

    我以为我找对问题了,其实不是这个原因。。。

    真正的原因是unique_ptr不能复制,换成shared_ptr就行了。这个很容易忽略。

    建议

    一般情况下,用shared_ptr就行了;

    如果非要用unique_ptr,一定要找准位置,最好用的范围小一些,不然很难看出问题出在哪里。

  • 相关阅读:
    八皇后(回溯经典)
    高精度阶乘(大数运算)
    跳棋(利用规范的数学方法)
    贪心砝码(分治法)
    大数乘方取余
    二分法查找
    汉诺塔(经典递归)(未完全明白)
    斐波那契数列和
    实验 7: OpenDaylight 实验——Python 中的 REST API 调用
    实验 6:OpenDaylight 实验——OpenDaylight 及 Postman 实现流表下发流表
  • 原文地址:https://www.cnblogs.com/cthon/p/9260975.html
Copyright © 2020-2023  润新知