简体   繁体   English

引用超类(C ++)中的子类

[英]Refering to subclasses in the superclass (C++)

I've got a problem with the implementation of my design. 我的设计实施存在问题。 There is a superclass defining methods, which have subclasses of this superclass as return values. 有一个超类定义方法,这些方法具有该超类的子类作为返回值。 As you can see here: 如您所见:

#ifndef I_ATTITUDEDESCRIPTOR_H
#define I_ATTITUDEDESCRIPTOR_H

#include "cl_EulerAngles.h"
#include "cl_Quaternion.h"
#include "cl_RodriguesParameters.h"
#include "cl_RotationMatrix.h"
#include "cl_TransformationMatrix.h"

class i_AttitudeDescriptor {
    public:
        i_AttitudeDescriptor();
        virtual ~i_AttitudeDescriptor() = 0;

        virtual cl_EulerAngles* getEulerAngles(cl_EulerAngles* result) = 0;
        virtual cl_Quaternion* getQuaternion(cl_Quaternion* result) = 0;
        virtual cl_RodriguesParameters* getRodriguesParameters(cl_RodriguesParameters* result) = 0;
        virtual cl_RotationMatrix* getRotationMatrix(cl_RotationMatrix* result) = 0;
        virtual cl_TransformationMatrix* getTransformationMatrix(cl_TransformationMatrix* result) = 0;
    protected:
    private:
};

#endif // I_ATTITUDEDESCRIPTOR_H

One of the subclasses is for example given by: 子类之一例如由以下给出:

#ifndef CL_EULERANGLES_H
#define CL_EULERANGLES_H

#include "i_AttitudeDescriptor.h"

#include "cl_Quaternion.h"
#include "cl_RodriguesParameters.h"
#include "cl_RotationMatrix.h"
#include "cl_TransformationMatrix.h"


class cl_EulerAngles : public i_AttitudeDescriptor
{
    public:
        cl_EulerAngles();
        ~cl_EulerAngles();

        cl_EulerAngles* getEulerAngles(cl_EulerAngles* result);
        cl_Quaternion* getQuaternion(cl_Quaternion* result);
        cl_RodriguesParameters* getRodriguesParameters(cl_RodriguesParameters* result);
        cl_RotationMatrix* getRotationMatrix(cl_RotationMatrix* result);
        cl_TransformationMatrix* getTransformationMatrix(cl_TransformationMatrix* result);
    protected:
    private:
};

#endif // CL_EULERANGLES_H

The functions are implemented in the cl_EulerAngles.cpp file. 这些功能在cl_EulerAngles.cpp文件中实现。 Now I got the problem, that I get a compiler error like this: 现在我有了问题,我得到了这样的编译器错误:

i_AttitudeDescriptor.h|17|error C2143: syntax error : missing ';' before '*'|
i_AttitudeDescriptor.h|17|error C2433: 'i_AttitudeDescriptor::cl_EulerAngles': 'virtual' not permitted on data declarations|
i_AttitudeDescriptor.h|17|error C4430: missing type specifier - int assumed|
i_AttitudeDescriptor.h|17|error C2061: syntax error : identifier 'cl_EulerAngles'|
i_AttitudeDescriptor.h|17|error C4430: missing type specifier - int assumed|
i_AttitudeDescriptor.h|17|warning C4183: 'getEulerAngles': missing return type; assumed to be a member function returning 'int'|
i_AttitudeDescriptor.h|17|error C2253: "i_AttitudeDescriptor::getEulerAngles": pure specifier only applies to virtual function – specifier ignored|
||=== Build finished: 6 errors, 1 warnings (0 minutes, 0 seconds) ===|

I'm hoping someone out there can help me fixing this little problem. 我希望外面有人可以帮助我解决这个小问题。 Thank you. 谢谢。

The compilation problem is because of the circular include directives. 编译问题是由于循环的include指令。 However, underneath the simple circular inclusion, there is a bigger design problem here. 但是,在简单的圆形包含之下,这里存在更大的设计问题。 Base class is not supposed to know the subclasses. 基类不应该知道子类。

Better solution: You should return a pointer to the Base class and use Polymorphism . 更好的解决方案:您应该返回一个指向Base类的指针,并使用Polymorphism

Not that good solution: If you really want to continue with your current design, you might consider not including the subclass headers, but forward declaring your subclasses in the base class header file. 并不是一个好的解决方案:如果您确实要继续当前的设计,则可以考虑不包括子类头,而是在基类头文件中声明子类。 This will solve the circular inclusion. 这将解决循环包含问题。

I would recommend sticking with the first solution. 我建议坚持第一个解决方案。

The problem here is circular includes; 这里的问题是循环包含; i_AttitudeDescriptor.h pulls in cl_EulerAngles.h , which pulls in i_AttitudeDescriptor.h . i_AttitudeDescriptor.h拉入cl_EulerAngles.h ,后者拉入i_AttitudeDescriptor.h Once the include guards kick in, you end up missing declarations, which seems to be what the compiler is complaining about. 一旦包含了保护措施,您最终会丢失声明,这似乎是编译器所抱怨的。

There are at least two solutions: 至少有两种解决方案:

  1. Forward declare the various types that are used in i_AttitudeDescriptor.h instead of #include ing their header files. 转发声明i_AttitudeDescriptor.h中使用的各种类型,而不是#include包括其头文件。 i_AttitudeDescriptor doesn't care about the details of those classes, just their names. i_AttitudeDescriptor并不关心这些类的详细信息,而只关心它们的名称。

  2. Declare all those virtual functions in i_AttitudeDescriptor as returning pointers to i_AttitudeDescriptor rather than pointers to the specific types. 声明在所有这些虚函数i_AttitudeDescriptor为返回指针i_AttitudeDescriptor ,而不是指向特定的类型。 You can still override them in derived classes with functions that return pointers to the derived types. 您仍然可以使用返回指向派生类型的指针的函数在派生类中覆盖它们。

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

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