"Implicit instantiation of undefined template" when forward declaring template class

benwad picture benwad · Oct 9, 2012 · Viewed 82.9k times · Source

I've got some code in which I need to forward-declare the a template class (or at least, forward-declaring would make things a lot easier for me...). I've written a simplified version of the problem I'm having so I can display it here:

template<bool>
class MyTemplateClass;

int main( int argc, char* argv[] )
{
    MyTemplateClass<false> myTemp;  // error here
    myTemp.GetTheValue();
    return 0;
}

template<bool bShouldMult>
class MyTemplateClass
{
    int m_myint;
    float m_myfloat;

public:
    MyTemplateClass() : m_myint(5), m_myfloat(3.0f) {}
    float GetTheValue()
    {
        return m_myint * (bShouldMult ? m_myfloat : 1.0f);
    }   

};

The error I'm getting at the commented line is:

Error - implicit instantiation of undefined template 'MyTemplateClass<false>'

What other detail do I need to include in a forward declaration of MyTemplateClass? Since the error isn't coming from the next line down I'm assuming it isn't due to the fact that the method is undefined. The compiler I'm using is LLVM/CLang, and I'm compiling on Mac.

Answer

kmiklas picture kmiklas · Mar 16, 2017

Did you forget to #include something?

I got this after forgetting to

#include <array>

When using an std::array

:^)