kellerprogger
kellerprogger

Reputation: 119

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’

But how is there an error?

Upvotes: 2

Views: 11990

Answers (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.

Upvotes: 7

Related Questions