error: Invalid use of incomplete type

橙三吉。 提交于 2020-07-08 03:11:26

问题


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’

But how is there an error?


回答1:


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



来源:https://stackoverflow.com/questions/38179919/error-invalid-use-of-incomplete-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!