0

So I'm trying to figure out how to make a Stack class with array implementation using values of the type double.

Heres the full header file...

#ifndef STACK_H
#define STACK_H

class Stack
{
private:
    double *stackArray;             //Pointer to the Stack array.
    int stackSize;                  //The size of stack.
    int top;                        //The first "plate" on top of the stack.
    
    void copy(const Stack &copy);  //Copy Constructor 
    void destroy();                 //The fxn the destructor calls.

public:
    Stack(int);                    //Constructor
    
    /*
    Stack(const Stack&);            //Copy Constructor 
    */
    ~Stack();                          //Destructor 
    
    
    //STACK OPERATIONS:
    void push(double);              //Adds a node on top of the stack. 
    
    void pop(double &);                     //Removes node on top of stack. 
    
    bool isEmpty() const;           //Returns true if stack is empty, false otherwise.
    
    bool isFull() const;            //Returns true if stack is full, false otherwise.
    
    double peek();                  //Gets the top item of the stack without removing the item. 
    
    int getSize();                  //Makes the array larger if the capacity of the array is being reached. 
};
#endif

And here's the full implementation file...

#include "Stack.h"
#include <iostream>
using namespace std;

Stack::Stack(int size)                    //The Constructor 
{
    stackArray = new Stack[size];       //Dynamically allocate memory for an array of a stack variable.
    stackSize = size;
    top = -1;                           //Set the top of the stack to -1. So its empty.
}


Stack::~Stack()                         //The destructor 
{
    destroy();                          //Calls the destroy function to delete the array.
}



  void Stack::push(double value)          //Adds a new 'plate' on the stack 
{
    if (isFull())
    {
        cout << "The stack is full.\n";         //Prints out a message saying the stack is already full 
    }
    else                                        //Otherwise...
    {
        top++;                                  //Increment top....
        stackArray[top] = value;                //...So we can make 'value' the new top in the array.
    }
}

void Stack::pop(double &value)                 //Removes a 'plate' from the stack 
{
    if (isEmpty())
    {
        cout << "The stack is empty.\n";
    }
    else                                        //Otherwise...
    {
        value = stackArray[top];                //Make 'value' the current top.
        top--;                                  //Removes the current value of top from the stack. 
    }
}


bool Stack::isEmpty() const
{
    bool status;                        //Tells the current status of the stack
    
    if (top == -1)                           //If theres nothing in the stack...
    {
        status = true;                 //...status returns true. 
    }
    else                              //Otherwise...
    {
        status = false;              //...The stack MUST have something already in it.
    }
    
    return status;                  
}


bool Stack::isFull() const
{
    bool status;                    
    
    if (top == stackSize - 1)               //Checks if the top of the stack is equal to the max stack size entered.
        status = true;                      //Returns true if stack is full.
    else
        status = false;                     //Or false if not.
    
    return status;          
}


void Stack::destroy()
{
    delete [] stackArray;           //Delete the Stack Array.
}


double Stack::peek()            //Gets the top item of the stack without removing item 
{
    return stackArray[top];
}


int Stack::getSize()                                        //Determines the size of the stack
{
    int numItems = 0;                                       //Variable to store number of items in stack.
    
    for (int index = 0; index < stackSize; index++)         //Goes through all the items in the stack....
    {
        numItems++;                                         //...and counts them.
    }
    
    return numItems;
}

/****
void copy(const Stack &copy)        //Deletes memory associated with stack
{
    
}
***/

The driver Looks like this....

   #include <iostream>
#include "Stack.h"
using namespace std;

int main()
{
    int stackSize;
    
    Stack stack1(10);
    
    cout << "Lets get the stack size!\n";
    cout << stack1.getSize();

    return 0;
}

My issue is that when I try to run this, It gives me the following error:

    Stack.cpp: In constructor ‘Stack::Stack(int)’:
Stack.cpp:13:32: error: no matching function for call to ‘Stack::Stack()’
     stackArray = new Stack[size];       //Dynamically allocate memory for an array of a stack variable.
                                ^
In file included from Stack.cpp:6:0:
Stack.h:20:9: note: candidate: Stack::Stack(int)
         Stack(int);                    //Constructor
         ^~~~~
Stack.h:20:9: note:   candidate expects 1 argument, 0 provided
Stack.h:9:7: note: candidate: constexpr Stack::Stack(const Stack&)
 class Stack
       ^~~~~
Stack.h:9:7: note:   candidate expects 1 argument, 0 provided
Stack.cpp:13:32: error: cannot convert ‘Stack*’ to ‘double*’ in assignment
     stackArray = new Stack[size];       //Dynamically allocate memory for an array of a stack variable.

Im not exactly sure what is going on here, if anyone can help me out that would be really great.

Also can someone give me a few tips on how should I approach the copy constructor and overloaded assignment operator for this class? Im not very good with those and am not sure how they would fit into this class implemenation.

Cyrus Lee
  • 27
  • 4

1 Answers1

1

This is the constructor u r trying to call

Stack::Stack(int size)                    //The Constructor 
{
    stackArray = new Stack[size];       //Dynamically allocate memory for an array of a stack variable.
    stackSize = size;
    top = -1;                           //Set the top of the stack to -1. So its empty.
}

Now, note that in the constructor you are allocating a dynamic array of Stacks and of size size.

Here stackArray = new Stack[size]


You have two problems

  1. The allocation uses the default constructor for the stack and you don't have one because you declared a custom one.
  2. If you use the custom constructor, you will have an infinite recursion.

You have to provide the right type of elements of the array that will be allocated (which, from the rest of the code, seems to be double) instead of Stack type to be

stackArray = new double[size].

asmmo
  • 6,922
  • 1
  • 11
  • 25