错误:无效使用不完整类型 [英] error: Invalid use of incomplete type

查看:298
本文介绍了错误:无效使用不完整类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题,有人有个好主意吗?

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();

};

}

错误是:

错误:无效使用了不完整的类型"Vector_2d类"

error: invalid use of incomplete type ‘class Vector_2d’

但是怎么会有错误呢?

But how is there an error?

推荐答案

class Vector_2d;这仅声明存在该名称的类.
要从中继承,必须提供完整的类定义.

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
    };
}

简单地说,当类B从类A继承时,类B的对象将具有A子对象作为其布局的一部分.
因此,如果您没有A的完整定义,则无法定义B的布局,该布局取决于A.

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.
So you can't define the layout of B, which depends on A, if you don't have the full definition of A.

这篇关于错误:无效使用不完整类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆