Grec Mihai
Grec Mihai

Reputation: 45

Error in array declaration: Expression must have a constant value

I declared a global struct with the following structure:

typedef struct {
    int value;
    int index;
}element;

I have a program with k sorted queues, and I put in a heapSort(I am using an array) the minimum from each one. I use the index value in struct to track the element that I want to pop out of the heap. Now, I want to test the program for different number of queues, so I did this:

for (int i = 10;i <= 50;i += 10) {
    const int k = i;
    element a[k];
}

But I get an error:

Expression must have a constant value

Is there any way I can "trick" that?

Upvotes: 0

Views: 240

Answers (1)

Andria
Andria

Reputation: 5075

You declared an array, an array's size cannot be changed. To solve this issue it would be in your best interest to use a vector.

Try this:

#include <vector>
vector <element> a(50);

To pop elements out of the vector try this:

a.erase(a.begin()+index_from_zero);

Upvotes: 1

Related Questions