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

时间:2022-01-27 20:10:21

I am having dependency troubles. I have two classes: Graphic and Image. Each one has its own .cpp and .h files. I am declaring them as the following:

我有依赖性麻烦。我有两个类:图形和图像。每个人都有自己的.cpp和.h文件。我将它们声明如下:

Graphic.h:


    #include "Image.h"
    class Image;
    class Graphic {
      ...
    };

Image.h:


    #include "Graphic.h"
    class Graphic;
    class Image : public Graphic {
      ...
    };

When I try to compile, I get the following error:

当我尝试编译时,我收到以下错误:

    Image.h:12: error: expected class-name before ‘{’ token

If I remove the forward declaration of Graphic from Image.h I get the following error:

如果我从Image.h中删除Graphic的前向声明,我会收到以下错误:

    Image.h:13: error: invalid use of incomplete type ‘struct Graphic’
    Image.h:10: error: forward declaration of ‘struct Graphic’

5 个解决方案

#1


10  

This worked for me:

这对我有用:

Image.h:

#ifndef IMAGE_H
#define IMAGE_H

#include "Graphic.h"
class Image : public Graphic {

};

#endif

Graphic.h:

#ifndef GRAPHIC_H
#define GRAPHIC_H

#include "Image.h"

class Graphic {
};

#endif

The following code compiles with no error:

以下代码编译时没有错误:

#include "Graphic.h"

int main()
{
  return 0;
}

#2


5  

You don't need to include Image.h or forward declare Image in Graphic.h - that's a circular dependency. If Graphic.h depends on anything in Image.h you need to split that out into a third header. (If Graphic has an Image member, that just isn't going to work.)

您不需要在Graphic.h中包含Image.h或forward declare Image - 这是一个循环依赖。如果Graphic.h依赖于Image.h中的任何内容,则需要将其拆分为第三个标头。 (如果Graphic有一个Image成员,那就不行了。)

#3


4  

Graphic.h doesn't need to include image.h, and it doesn't need to forward declare the Image class. Also, Image.h doesn't need to forward declare the Graphic class since you #include the file that defines that class (as you must).

Graphic.h不需要包含image.h,也不需要转发声明Image类。此外,Image.h不需要转发声明Graphic类,因为#include定义该类的文件(如您所愿)。

Graphic.h:

class Graphic {
  ...
};

Image.h:

#include "Graphic.h"
class Image : public Graphic {
  ...
};

#4


1  

Since Image extends Graphic, remove the inclusion of Image in your Graphic.h file.

由于Image扩展了Graphic,因此在Graphic.h文件中删除Image的包含。

Graphic.h

class Graphic {
  ...
};

#5


0  

First remove this, you must always have the complete class definition available in order to inherit from a class:

首先删除它,您必须始终拥有完整的类定义才能从类继承:

class Graphic;

Second, remove all references to Image from Graphic.h. The parent will usually not need to know of its childs.

其次,从Graphic.h中删除对Image的所有引用。父母通常不需要知道其孩子。

#1


10  

This worked for me:

这对我有用:

Image.h:

#ifndef IMAGE_H
#define IMAGE_H

#include "Graphic.h"
class Image : public Graphic {

};

#endif

Graphic.h:

#ifndef GRAPHIC_H
#define GRAPHIC_H

#include "Image.h"

class Graphic {
};

#endif

The following code compiles with no error:

以下代码编译时没有错误:

#include "Graphic.h"

int main()
{
  return 0;
}

#2


5  

You don't need to include Image.h or forward declare Image in Graphic.h - that's a circular dependency. If Graphic.h depends on anything in Image.h you need to split that out into a third header. (If Graphic has an Image member, that just isn't going to work.)

您不需要在Graphic.h中包含Image.h或forward declare Image - 这是一个循环依赖。如果Graphic.h依赖于Image.h中的任何内容,则需要将其拆分为第三个标头。 (如果Graphic有一个Image成员,那就不行了。)

#3


4  

Graphic.h doesn't need to include image.h, and it doesn't need to forward declare the Image class. Also, Image.h doesn't need to forward declare the Graphic class since you #include the file that defines that class (as you must).

Graphic.h不需要包含image.h,也不需要转发声明Image类。此外,Image.h不需要转发声明Graphic类,因为#include定义该类的文件(如您所愿)。

Graphic.h:

class Graphic {
  ...
};

Image.h:

#include "Graphic.h"
class Image : public Graphic {
  ...
};

#4


1  

Since Image extends Graphic, remove the inclusion of Image in your Graphic.h file.

由于Image扩展了Graphic,因此在Graphic.h文件中删除Image的包含。

Graphic.h

class Graphic {
  ...
};

#5


0  

First remove this, you must always have the complete class definition available in order to inherit from a class:

首先删除它,您必须始终拥有完整的类定义才能从类继承:

class Graphic;

Second, remove all references to Image from Graphic.h. The parent will usually not need to know of its childs.

其次,从Graphic.h中删除对Image的所有引用。父母通常不需要知道其孩子。