0

I made a template class Block and wanted to cache some data for each instance type of Block. For this reason I created an inner class Cache which is storing and initialising the data often used by Block class instances. But the compiler (Visual Studio 2017, C++17) is not accepting the static member variable definition and the error message is not helping me. Whats wrong with my code and how I can accomplish my goal?

Errors:
Error C2061 syntax error: identifier 'Cache'

Warnings:
Warning C4346 'Cache': dependent name is not a type

template<int D>
class Block
{
private:
    class Cache
    {
    public:
        int mData[D];
    public:
        Cache();
    };
private:
    static Cache mCache;
public:
    Block();
};

template<int D>
Block<D>::Cache Block<D>::mCache; // Syntax error "Cache"!

template<int D>
inline Block<D>::Block()
{
    // Use cache data.
    cout << Block<D>::mCache.mData[0] << endl;
}

template<int D>
inline Block<D>::Cache::Cache()
{
    // Initialise cache data.
    mData[0] = D;
}
Michael
  • 595
  • 5
  • 19

1 Answers1

2

add typename

template<int D>
typename Block<D>::Cache Block<D>::mCache;

You can read https://en.cppreference.com/w/cpp/language/dependent_name, but put it short, compiler don't know if it's a type.

apple apple
  • 10,292
  • 2
  • 16
  • 36