简体   繁体   English

如何对来自不同头文件的类使用多态?

[英]How to use polymorphism with classes from different header files?

I've got class Figure in my header "figure.h": 我的头文件“ figure.h”中包含类Figure:

class Figure
{
    Color color;
    std::string position;
    ...
};

and I want to define a class King in header "king.h". 我想在标题“ king.h”中定义一个类King。 So in king.h I do #include "figure.h" , and write for ex: 因此,在king.h中,我#include "figure.h" ,并为ex编写:

class King : public Figure
{
     char type;
     bool checkIfTypeIsValid(std::string);
     ...
};

But this doesn't seem to work, as King doesn't recognize Figure... What should I do? 但这似乎不起作用,因为King无法识别Figure ...我该怎么办? And is it smart to have different headers for different inherited classes, or just lump them together in "figure.h"? 对于不同的继承类具有不同的标头,或者只是将它们一起放在“ figure.h”中,是否明智? Because I'll have a Queen, Bishop, etc. figures as well, which will make quite a lot of headers and impl. 因为我还将有一个Queen,Bishop等人物,这将产生很多标题和隐含的内容。 files.. 文件..

First: you cannot derive from an unknown, or even a partially known class. 第一:您不能从未知的甚至是部分已知的类派生。 In order to define King , you must first include Figure.h (supposing King is defined in a different file). 为了定义King ,必须首先包括Figure.h (假设King是在其他文件中定义的)。

More generally, as to how to organize the code, there is no one definitive answer. 更一般地,关于如何组织代码,没有一个明确的答案。 Personally, in the one particular case (where the number of derived classes is well defined and strictly bound), I'd probably cut corners and define all of them in Figure.h , but there are very good arguments for using a separate header for each—if nothing else, you don't have to include the headers for the individual figures except in files where they are created. 就个人而言,在一种特殊情况下(派生类的数量已定义并严格限制),我可能会偷工减料并在Figure.h定义所有它们,但是对于将单独的标头用于每个-如果没有其他要求,则无需在创建图的文件中包括各个图形的标题。 Another solution would be to use factory functions for the figures, and define the derived classes in the source file which implements the factory functions. 另一种解决方案是将工厂功能用于图形,并在实现工厂功能的源文件中定义派生类。

You need include figure.h at first. 首先需要包含figure.h

#include<Figure.h>

class King : public Figure
{
    ...
};

Check include path for your compiler. 检查编译器的包含路径。 It would be like : -I"PATH_TO_DIR_WHERE_FIGURE_H_LOCATED", without quotes. 就像:-I“ PATH_TO_DIR_WHERE_FIGURE_H_LOCATED”,不带引号。

Ensure that king.h contains #include "figure.h" or #include <figure.h> , as long as figure.h is on your include path on in the same folder as king.h . 请确保king.h包含#include "figure.h"#include <figure.h> ,只要figure.h位于您的include路径中,并且与king.h处于同一文件夹中。 If you're on Windows, the include headers can have incorrect case. 如果您使用的是Windows,则包含标头的大小写可能不正确。 On everything else, ensure that the case is correct. 在其他所有方面,请确保情况是正确的。 Lastly, if the definition of Figure is inside a namespace, you'll have to append that: 最后,如果Figure的定义在名称空间中,则必须附加以下内容:

class King : public some_namespace::Figure
{
   //....
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM