- 一、问题描述
- 二、解决方法
一、问题描述
A.h
#ifndef A_H
#define A_H
#include "B.h"
class A{
typedef vector::sizetype size_type;
B b;
}
#endif
B.h
#ifndef B_H
#define B_H
#include "A.h"
class B{
A::size_type num;
}
#endif
A.h和B.h相互包含会编译错误
引入前置声明
- 把A中的B转成B*;
- class A中先声明B,也就是前置声明;
- A.h 移除对类B.h文件的包含(若用了#ifdef则不必须)
A.h
#ifndef A_H
#define A_H
class B //前置声明
class A{
typedef vector::sizetype size_type;
B* b;
}
#endif
B.h
#ifndef B_H
#define B_H
#include "A.h"
class B{
A::size_type num;
}
#endif



