我有依赖问题。我有两个类:Graphic
和 Image
。每个都有自己的 .cpp 和 .h 文件。我声明它们如下:
图形.h
:
#include "Image.h"
class Image;
class Graphic {
...
};
Image.h
:
#include "Graphic.h"
class Graphic;
class Image : public Graphic {
...
};
当我尝试编译时,出现以下错误:
Image.h:12: error: expected class-name before ‘{’ token
如果我从 Image.h
中删除 Graphic
的前向声明,我会收到以下错误:
Image.h:13: error: invalid use of incomplete type ‘struct Graphic’ Image.h:10: error: forward declaration of ‘struct Graphic’
最佳答案
这对我有用:
图片.h:
#ifndef IMAGE_H
#define IMAGE_H
#include "Graphic.h"
class Image : public Graphic {
};
#endif
图形.h:
#ifndef GRAPHIC_H
#define GRAPHIC_H
#include "Image.h"
class Graphic {
};
#endif
下面的代码编译没有错误:
#include "Graphic.h"
int main()
{
return 0;
}
关于c++ - 如何从不同头文件中的类继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/253284/