关于C++项目中头文件相互包含的问题

我们有时会写这样的头文件(为了简述,省略头文件保护)

  head1.h

#include "head2.h"

class head1 {
  //用到了class head2
}

#include "head1.h"

class head2 {
  //用到了class head1
}

相互包含之后,预编译展开后可能会导致head2的声明在head1之后,或者head1的声明在head2之后,因此编译器就会报错class head1/class head2没有声明或者未定义.

解决方法:

1.避免头文件的相包含

2.增加前置声明

原文地址:https://www.cnblogs.com/yxsrt/p/12589548.html