简体   繁体   English

错误:无效使用不完整类型

[英]error: Invalid use of incomplete type

I got the following problem, does anyone have a good idea? 我遇到以下问题,有人有个好主意吗?

class Vector_2d;

namespace Utils {

class Align_vector : public Vector_2d {
protected:
    bool check_range(int x, int y);

public:
    enum alignment {left, right, up, down};

    Align_vector(Alignment alignment);
    void set_alignment(Alignment alignment);
    Alignment get_alignment();

};

}

the error is: 错误是:

error: invalid use of incomplete type 'class Vector_2d' 错误:无效使用不完整类型'Vector_2d类'

But how is there an error? 但是怎么会有错误呢?

class Vector_2d; This only declares a class by that name exits. 这仅声明存在该名称的类。
To inherit from it, the full class definition needs to be available. 要从中继承,需要提供完整的类定义。

class Vector_2d {
  // Your code goes here
};

class Align_vector : public Vector_2d {
  // Other stuff
};

If you have separate header files for these classes, be sure to include it before defining the class that inherits. 如果这些类具有单独的头文件,请确保在定义继承的类之前将其包括在内。

#include <vector_2d.h>

namespace Utils {
    class Align_vector : public Vector_2d {
      // Other stuff
    };
}

To put it simply, when class B inherits from class A , objects of class B will have an A sub-object as part of their layout. 简单地说,当类B从类继承A ,该类的对象B将有A子对象作为其布局的一部分。
So you can't define the layout of B , which depends on A , if you don't have the full definition of A . 所以,你不能定义布局B ,这取决于A ,如果你不具备的完整定义A

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

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