头文件循环引用的错误

Posted cthon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了头文件循环引用的错误相关的知识,希望对你有一定的参考价值。

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
















以上是关于头文件循环引用的错误的主要内容,如果未能解决你的问题,请参考以下文章

错误错误 522:循环引用

为啥iOS的Masonry中的self不会循环引用

XSD.exe 和“循环组引用”

Dagger 2 - 为啥我收到循环引用错误?

由于循环依赖错误,无法添加对项目的引用

导入错误。循环引用