• Register

This error usually occurs when the array size is not constant at compile time.

Example:

For example, if the user is trying to implement a stack using an array;

class Stack{

private:

    int cap;

    int elements[this->cap]; // <--- Errors here

    int top;

public:

  Stack(){

     this->cap=5;

     this->top=-1;

};

Output:

This will display the following output;

Multiple markers at this line

- invalid use of 'this' at the top level

- array bound is not an integer constant before ']' token

Solution1:

In C, the size of an array must be a constant known at the compile time. You will get an error if that is not in any case.

Here, the user has;

int elements[this->cap];

Notice this->cap is not constant known at the compile time since it depends upon the size of the cap. If you have a variable size array whose size can be determined later then use std:: vector that can be resized at run time.

Solution 2:

This error can also be solved by using the pointer implementation by;

Changing

int elements[this->cap];

To

int* elements = new int[cap];

I hope this will help to solve your error.

posted Jul 8 in c 3,890 points