c++ - 如何从不同头文件中的类继承?

标签 c++ inheritance organization

我有依赖问题。我有两个类:GraphicImage。每个都有自己的 .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/

相关文章:

python - 类属性的继承?

java - 如何在多个 GWT eclipse 项目之间共享代码?

c++ - 处理模棱两可的类型转换/转换 C++11 的正确方法是什么

c++ - 将额外的依赖项与 XCode 链接起来

c++ - 如何将数据库中的数据存储在多维数组中

python - 为不同版本的 Python 维护不同版本代码库的工作流

c# - 使用 WinForms 组织

c++ - 错误 : ‘hour’ was not declared in this scope

Java继承层次动物类

C++ | DLL/EXE - 如何从导出类中调用另一个类方法?