Question

I'm trying to initialize an array called ARRAY inside the constructor of a class called CLASS_A and this array is supposed to be accessible by the nested class CLASS_B. I thought of making the array a template but that was when things started to get a little fuzzy with how to go about the syntax.

I'm almost certain that declaring the ARRAY static is part of the solution and maybe making the type const int a template like A so then the template would look like template <class A, const int D> and the declaration A ARRAY[SIZE];. Anyways I initially got this error compiling the code below and then I made some changes and got a different error.

This is a linker error by the way

nested_class_incomplete_type.cpp|16|undefined reference to `CLASS_A<3>::ARRAY'|
nested_class_incomplete_type.cpp|28|undefined reference to `CLASS_A<3>::ARRAY'|

edit duplicate code.

Was it helpful?

Solution

You only declared ARRAY in CLASS_A, you need to define it:

template <const int D>
int CLASS_A<D>::ARRAY[SIZE] = {};

BTW, your SIZE field is redundant, you don't need it, e.g:

template <size_t N>
class CLASS_A
{
public:
    static int ARRAY[N];
    //.....
};

template <size_t N>
int CLASS_A<N>::ARRAY[N] = {};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top