Nickal
Nickal

Reputation: 737

When does an Incomplete Type error occur in C++

Can anyone tell me when does a c++ compiler throw an "incomplete type error"?

Note: I have intentionally left this question a little open ended so that I can debug my code myself.

Upvotes: 27

Views: 61698

Answers (5)

Bozla
Bozla

Reputation: 1

This article helped me: https://hatchjs.com/incomplete-type-is-not-allowed-c/

You can also use a include directive to include the header file that defines the MyClass type:

include “myclass.h”

Upvotes: -1

Mike Lischke
Mike Lischke

Reputation: 53315

This happens usually when the compiler has seen a forward declaration but no full definition of this type, while the type is being used somewhere. For example:

class A;

class B { A a; };

The second line will cause a compiler error and, depending on the compiler, will report an incomplete type (other compilers give you a different error, but the meaning is the same).

When you however just use a pointer to such a forward declaration no complain will come up, since the size of a pointer to a class is always known. Like this:

class A;
class B {
   A *a;
   std::shared_ptr<A> aPtr;
};

If you ask what could be wrong in a concrete application or library when this error comes up: that happens usually when a header is included which contains the forward declaration, but full definition hasn't been found yet. The solution is quite obvious: include also the header that gives you access to the full type. Sometimes you may also simply have no or the wrong namespace used for a type and need to correct that instead.

Upvotes: 45

Mykola Khyliuk
Mykola Khyliuk

Reputation: 1403

This also happens when you use forward declaration with std::unique_ptr (for example, to implement PIMPL idiom) in your class with a default destructor, which leads to such issue.

It is good explained here: Forward declaration with unique_ptr?

Upvotes: 3

Abhishek Agarwal
Abhishek Agarwal

Reputation: 9

In my case, it was due to poor knowledge of templates. I declared a class between a template definition and the function which was associated with that template.

template<typename T>
class
{
  foo a;
  foo b;
};
function(T a,int b)
{

 . . . . .

}

And this created issues as the template definition is associated with the class, in this case, an error comes in the parameter list of the function that T is not defined and also that incomplete type is not allowed. If you have to use a template for multiple entities, then you have to reuse this statement before that entities' definition:

template<typename T>

Upvotes: 1

calceamenta
calceamenta

Reputation: 41

This happens when we try to use a class/object or its methods and they have not been defined yet. For example

class A;
class B {
    class A obj;
}

or

class A;
class B {
    class A *obj;
    B() {   
           obj->some_method();
        }

To resolve this, A has to be defined first or its total declaration has to given(best practice is to do it in a header file) and all methods of both the classes should be defined later(best practice is to do it in another file).

class A {
    //definition
}
class B {
class A obj;
}

Upvotes: 3

Related Questions