Home > Enterprise >  My st.show on stack cannot be display for some reason
My st.show on stack cannot be display for some reason

Time:11-12

My problem on my code is when i run it, it says the error no matching function call to stack::show() which i have. i dont know whats causing the error, did some research that it should be on the public class which already there.

I used switch case 1 is to input 2nd is to show or to display the user inputs which I cant call out using st.show();

    private:
        int MAX;
        int top;
        string *arr_q;
    
    public:
        Stack (int size)
        {
            MAX=size;
            top=-1;
            arr_q=new string[MAX];
        }
        void push(string subs)
        {
        if ((top 1)==MAX)
            cout << "Stack Overflow..." << endl;
        top=top 1;
        arr_q[top]=subs;
        }
    
        void show(int lim)
        {
        int i = lim;
        cout<<"Stack contains --"<<endl;
        for (int ctr=i; ctr>=0; ctr--)
        {
            if (ctr==i)
                cout<<"\t\t"<<arr_q[ctr]<<"<--Top of Stack"<<endl;
            else
                cout<<"\t\t"<<arr_q[ctr]<<endl;
        }
        }
    };
    
    case 2: {
    st.show();
    break;

CodePudding user response:

The error is that your show function has a parameter but when you call it there is no parameter.

Seems reasonably likely that show should be written without a parameter. Like this

void show()
{
    cout<<"Stack contains --"<<endl;
    for (int ctr=top; ctr>=0; ctr--)
    {
        if (ctr==top)
            cout<<"\t\t"<<arr_q[ctr]<<"<--Top of Stack"<<endl;
        else
            cout<<"\t\t"<<arr_q[ctr]<<endl;
    }
}

CodePudding user response:

private:
    int MAX;
    int top;
    string *arr_q;

public:
    Stack (int size)
    {
        MAX=size;
        top=-1;
        arr_q=new string[MAX];
    }
    void push(string subs)
    {
    if ((top 1)==MAX)
        cout << "Stack Overflow..." << endl;
    top=top 1;
    arr_q[top]=subs;
    }

    void show(int lim)
    {
    int i = lim;
    cout<<"Stack contains --"<<endl;
    for (int ctr=i; ctr>=0; ctr--)
    {
        if (ctr==i)
            cout<<"\t\t"<<arr_q[ctr]<<"<--Top of Stack"<<endl;
        else
            cout<<"\t\t"<<arr_q[ctr]<<endl;
    }
    }
};

case 2: {
st.show();
break;
  • Related