I've checked posts here that I can use template for nested struct. But when I'm trying to initialize an array inside a nested struct, there seems problem during initialization. In the following example, the array size is one of the parameters of the nested struct so Visual Studio complained that the array size is illegal. Here is the definition:
//template<typename U, typename T>
template<typename U, size_t T> // this line also not work
struct A {
struct B {
struct C {
vector<U> count[T]; // count needs to have variable size
C() {
count = new U[T]; // allocate size. Not work
}
C c;
};
B b;
};
Did I do anything wrong when using the template and initialize the array?
Thanks
countas an array, then trying to initialize it with a pointer. This is not valid. you can reproduce this by doingint x[5]; x = new int[5];and get the same error. What exactly are you trying to do? Should the array size be fixed? Or dynamic?count[T]attempts to use the name of a typeTas the size of an array.vector<U> count[T]is a C-style array of (vectors of Us) with size T. I'm pretty sure you should havestd::array<U, T>there and then you don't need a constructor.