I'm trying to make an adjacency list graph implementation using vectors. In my vector declaration, I keep getting an error of "expression must have a constant value." I don't understand why I get this error, as I thought that vectors were already dynamic arrays and this wouldn't be a problem.
int nodes = 5;
vector<int> adjacencyList[nodes];
The size of an array variable must be compile time constant, regardless of the type of the element of the array.
It is somewhat unclear whether your array declaration was intentional. If you want a dynamic array of vectors, then what you could use instead is a vector of vectors. If your intention was to create a single vector, then don't use the square brackets since the square brackets are syntax for an array. Here is an example of how to initialise a single vector of particular size:
std::vector<int> adjacencyList(nodes);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With