'I started learning DSA ,while writing code for stack DS in c++ , I came across this error

I started learning DSA ,while writing code for stack DS in c++ , I came across this error. So, while I was trying fixing it , I get to know that when I am using "static const int n= 5;" instead of "static int n= 5;" or "const int n= 5;" or just " int n= 5;" my program was running without any error. I want to understand why "static int n= 5;" or "const int n= 5;" or just " int n= 5;" is not working for this program.I was trying adding a constant. So, that if I wanted to change number of elements in the stack I can easily do it

#include<iostream>
#include<string>

using namespace std;

class stack
{
    private:
    int top;
    const int n = 5;
    int arr[n];
    public:
    stack()  // constructor 
    {
        
        for(int i=0;i<n;i++)
        {
            arr[i]=0;
        }
    }
    bool isEmpty()
    {
        if (top==-1)
            return true;
        else
            return false;

    }
    bool isFull()
    {
        if(top == (n-1))
            return true;
        else
            return false;
    }
    void push(int value)
    {
        if(isFull())
            cout<<"stack is full \npush operation can't be performed "<<endl;
        else
        {
            top++;
            arr[top]=value;
        }
    }
    int pop()
    {
        if(isEmpty())
        {
            cout<<"stack is empty \n pop operation can't be performed "<<endl;
            return 0;
        }
        else
        {
            int popValue=arr[top];
            arr[top]=0;
            top--;
            cout<<"popped value is :"<<popValue<<endl;
            return popValue;
        }
    }
    int count()
    {
        return top+1;
    }
    int peek(int position)
    {
        if(isEmpty())
            cout<<"stack underflow "<<endl;
        else
            return arr[position];
    }
    void change(int position , int value)
    {
        if(isEmpty())
            cout<<"stack is empty \nchange operation can't be performed"<<endl;
        else if(position > top)
            cout<<"there is no item on "<<position<<" position \n So, change operation can't be performed "<<endl;
        else
        {
            arr[position]=value;
            cout<<" value changed at position "<<position<<endl; 
        }
    }
    void display()
    {
        cout<<" all values in the stack are :"<<endl;
        for(int i=(n-1);i >= 0;i--)
        {   cout<<arr[i]<<" ";
            cout<<"\n";
        }
    }
};  // class terminated
int main()
{
    stack s1;
    int option,position,value;

    do  //using do-while loop for menu driven programm 
    {
        cout<<"Enter the operation you want to perform. \nSelect option number.\n   Enter 0 to exit "<<endl;
        cout<<"1. push()"<<endl;
        cout<<"2. pop()"<<endl;
        cout<<"3. isFull()"<<endl;
        cout<<"4. isEmpty"<<endl;
        cout<<"5. peek()"<<endl;
        cout<<"6. count()"<<endl;
        cout<<"7. change()"<<endl;
        cout<<"8. display()"<<endl;
        cout<<"9. clear screen"<<endl<<endl;
        cin>>option;

        switch(option)
        {
            case 0:
                break;
            case 1:
                cout<<"push function is called \n";
                cout<<" Enter the value to be pushed :";
                cin>>value;
                s1.push(value);
                break;
            case 2:
                cout<<"pop function is called \n";
                s1.pop();
                break;
            case 3:
                if(s1.isFull())
                    cout<<"Stack is full"<<endl;
                else
                    cout<<"Stack is not full"<<endl;
                break;
            case 4:
                if(s1.isEmpty())
                    cout<<"Stack is empty"<<endl;
                else
                    cout<<"Stack is not empty"<<endl;
                break;
            case 5:
                cout<<"Enter the position you want to peek :";
                cin>>position;
                cout<<"the value at position "<<position<<" is "<<s1.peek(position)<<endl;
                break;
            case 6:
                cout<<"Number of elements is the Stack is :"<<s1.count()<<endl;
                break;
            case 7:
                cout<<"Change funtion is called "<<endl;
                cout<<"Enter the position you want to change :";
                cin>>position;
                cout<<"Enter the value you want to be changed to :";
                cin>>value;
                s1.change(position,value);
                break;
            case 8:
                cout<<"Display function is called "<<endl;
                s1.display();
                break;
            case 9:
                system("cls");
                break;
            default:
                cout<<"enter proper option number"<<endl<<endl;        
        }  // SWITCH STATEMENTS IS TERMINATED

    }while(option != 0);  //  DO-WHILE LOOP IS TERMINATED

    return 0;
}


Solution 1:[1]

int arr[n];

In C++ arrays must have a size that is known at compile time. For a class variable that means n must be a global constant or static constant.

And you should read up on std::array and std:vector as better alternatives.

Solution 2:[2]

static const: “static const” is basically a combination of static (storage specification) and cons (type of fit).

Static: determines the lifespan and appearance / accessibility of flexibility. This means that if the variable is declared as a static variable, it will remain in memory the entire time the system is running, while normal or automatic variables are destroyed when the function call (where the variable is defined) is over.

Const: is a form of fitness. Type suitability is used to display additional information about value by system type. If a variable is activated using a constitutions type, it will not accept additional changes in its value. So combining static with const, we can say that if the variable is implemented using static const, it will keep your value until the system is used again and again, it will not accept any change in its value.

Solution 3:[3]

The problem is that in standard C++ the size of an array must be a compile-time-constant. This means the following is incorrect in your given program:

class stack
{
    const int n = 5;
    int arr[n]; //NOT STANDARD C++ because n is not a constant expression
};

To solve this you can make n a constant expression as shown below:

class stack
{
//--vvvvvvvvvvvvvvvv-------------->static constexpr added. You can even write static const int n = 5;
    static constexpr int n = 5; 
    int arr[n]; //works now because `n` is a constant expression
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Goswin von Brederlow
Solution 2
Solution 3 Anoop Rana