今天项目中,遇到一个很奇怪的问题,明明导入了头文件,却一直说不知道该类型。
如类 VideoInfo 类存储的是视频 的信息参数,它当作参数,传入 解码器,作为解码器的成员变量,如下:
#include <cstdint>
#include "IDecode.h"
extern "C"{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
};
class VideoInfo {
public:
VideoInfo();
void printVideoInfo();
public:
const char *url;
AVFormatContext *formatContext;
IDecode *decode;
};
而解码器IDecode代码:
#include "LPacket.h"
#include "VideoInfo.h"
class IDecode {
public:
IDecode(){};
virtual void initDecode(LPacket *lPacket)=0;
virtual void decode(LPacket *lPacket)=0;
void sendFrame(void *frame, PacketType packetType);
public:
VideoInfo *videoInfo;
就会报出,VideoInfo 中找不到IDecode类型,在IDecode找不到VideoInfo ,原因在于,它们的头文件互相调用,并且都包含互相类对象作为成员变量,从而 在初始化一个变量的时需要另外一个变量,发现需要自身初始化完毕后,才能初始化另外一个变量,从而编译失败。
总结:c++ 无法将2个类互相为成员变量,若想这样,就得以一个为void的指针形式,作为成员变量,然后在cpp 文件中,转化为成员变量的指针类型就可以了。