“Implicit instantiation of undefined template” when forward declaring template class

In order to declare a variable of any type, template or not, the entire definition of that type must be available. You cannot forward-declare a template, and then start using it as if it were defined. All you can do at that point is declaring a pointer to an object of a type based on the template, like this:

MyTemplateClass<false> *myTempPtr;  // No error here

Unfortunately (but not unexpectedly) this moves the error to the next line. The problem of initializing that pointer remains: once you attempt to invoke new MyTemplateClass<false>, you will see an error.

You need to re-arrange your code to move the definition of the template ahead of its place of use. This may be somewhat tedious, but there is no way around it: the compiler needs to have the entire definition at the point where you start instantiating your template and calling its methods.

Leave a Comment