CamAd
CamAd

Reputation: 41

Incomplete type in c++

I have declared DynArray var_name in c++, but while compiling it gives me an error "incomplete type not allowed" I tried to search but nothing came up with an example of dynamic array. Could anyone please explain the error? Thanks.

Upvotes: 0

Views: 354

Answers (2)

Preet Kukreti
Preet Kukreti

Reputation: 8607

If you are defining a member variable x to your class C in a header file C.h , you must include the header of the class X (X.h) in C.h . If however, you are just storing a pointer to X as a member of C, then you can forward declare class X; before you declare class C{...}; and then in the definition file C.cpp, you must include X.h if you access any members of x.

Upvotes: 2

Andrew Tomazos
Andrew Tomazos

Reputation: 68618

"incomplete type not allowed" usually means that a class has been declared but not defined at a point at which its complete definition is needed.

This sometimes happens because of a circular dependency in the headers,

class A's definition depends on class B's definition and visa-versa - causing A.h to include B.h, and then B.h try's to include A.h but can't because the header guard is already defined, so it skips, but then only gets a fwd decl of A.

Upvotes: 0

Related Questions