If at all, you need to write this->maxSize = maxSize; since this is a pointer.
But itβs better not to write this at all and instead use a list of initializer constructors:
explicit Stack(int m) : contents(new stackElement[m]), top(-1), maxSize(m) {
I also added explicit so that you don't accidentally convert 5 to Stack .
You also incorrectly specified array initialization.
In addition, you do not need to verify that contents not null: when new fails, it leaves the exception, it does not return a null pointer. (This behavior does not make sense if you think about objects.)
It is very important to note that you have no more than one bare new -expression in the constructor. Everything else is a disaster with an exception-security and a sign that you need to reorganize and use resource management classes with one responsibility.
The destructor should be simple: ~Stack() { delete [] contents; } ~Stack() { delete [] contents; } Everything else is meaningless waste.
Imagine that you had to pay for each line of code that you write. Be patient, lose the source, think.
source share