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