头文件循环引用的错误

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,一定要找准位置,最好用的范围小一些,不然很难看出问题出在哪里。

posted @ 2018-07-03 22:19  CTHON  阅读(993)  评论(0编辑  收藏  举报