Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:17:08+00:00 2026-05-27T15:17:08+00:00

My first ever question here. Please excuse me, I have just entered into C++

  • 0

My first ever question here. Please excuse me, I have just entered into C++ and was starting up with DS. STACK!!!

My code: I think

    using namespace std;
    typedef char stackElement;

    class Stack
    {
    public:
        stackElement *contents;   //dynamically allocated: as we do not know what would be the size of our array.
        int top, maxSize;               // current Top index in the array

                //max size of the array; we need it to know if the array is full

    Stack(int maxSize)
    {

        contents = new stackElement(maxSize);
        this.maxSize = maxSize;
        if(contents == NULL)
        {
            cout<<"Insufficient memory";
            exit(1);

        }

        top = -1;
    }
    ~Stack()
    {
        delete [] contents;
        contents = NULL;
        maxSize = 0;
        top = -1;

    }

    bool isEmpty()const
    {

        return top < 0;
    }
    bool isFull() const
    {

        return top == maxSize - 1;
    }


    void push(stackElement element)
    {
        if(isFull())
        {
            cout<<"STACK IS ALREADY FULL";
            exit(1);
        }
        top = top + 1;
        contents[top] = element;
    }


};

int main()
{

    cout<<"STACK IMPLEMENTATION";
    int i = 1;
    Stack s1(i);


    s1.push('a');
    s1.push('1');
    return 0;
}

I am getting this error:

error: request for member 'maxSize' in 'this', which is of non-class type 'Stack* const'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:17:09+00:00Added an answer on May 27, 2026 at 3:17 pm

    If at all, you’d have to write this->maxSize = maxSize;, since this is a pointer.

    But better not to write that at all and instead use the constructor-initializer list:

      explicit Stack(int m)
      : contents(new stackElement[m]), top(-1), maxSize(m)
      {
          // nothing else to do
      }
    

    I also added explicit so you don’t accidentally convert 5 into a Stack.

    You also wrote the array initialization wrong.

    Also, you don’t need to check that contents is not null: When new fails, it exits with an exception, it does not return a null pointer. (That behaviour would make no sense when you think in terms of objects.)

    It is crucial to note that you have at most one naked new-expression in your constructor. Anything else is an exception-safety disaster, and a sign that you need to refactor and use single-responsibility resource-managing classes.


    The destructor should just be: ~Stack() { delete [] contents; } Everything else is pointless waste.


    Imagine you had to pay for every line of code you write. Be patient, lose the source, think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

newcomer and first ever question here. I am using the multiprocessing module of Python
This is probably the dumbest question ever here on stack Overflow. But I am
this is my first question here at Stack Overflow. I've been looking for a
Hi guys this is my first question ever on SO so please go easy
I have my first ever interview for a Java developer role, specifically RMI, Serverlets
This is the first time ever I'm using AJAX, and I want to do
Here is a problem I've struggled with ever since I first started learning object-oriented
I have recently started learning F#, and this is the first time I've ever
First off, I am using Windows XP. I have multiple hard drives and it
this is my first question here so sorry if I do something wrong or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.