Getting error “array bound is not an integer constant before ‘]’ token”

In C++, the size of an array must be a constant known at compile-time. You’ll get an error if that isn’t the case.

Here, you have

int elements[this->cap];

Notice that this->cap isn’t a constant known at compile-time, since it depends on how big cap is.

If you want to have a variably-sized array whose size is determined later on, consider using std::vector, which can be resized at runtime.

Hope this helps!

Leave a Comment