【问题标题】:C++ Stacks with Array Implementation for Values of type double具有双精度类型值的数组实现的 C++ 堆栈
【发布时间】:2021-06-01 16:27:19
【问题描述】:

所以我试图弄清楚如何使用 double 类型的值创建一个具有数组实现的 Stack 类。

这是完整的头文件...

#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

这是完整的实现文件...

#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
{
    
}
***/

司机长这样……

   #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;
}

我的问题是,当我尝试运行它时,它给了我以下错误:

    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.

我不确定这里发生了什么,如果有人可以帮助我,那就太好了。

还有人可以给我一些关于我应该如何处理这个类的复制构造函数和重载赋值运算符的提示吗?我对这些不太好,不确定它们如何适应这个类的实现。

【问题讨论】:

  • new double[size].
  • 这个简单的程序你会遇到更大的问题:int main() { Stack stack1(10); Stack stack2 = stack1; } -- main 末尾出现双重删除错误。

标签: c++ class compiler-errors stack implementation


【解决方案1】:

这是你试图调用的构造函数

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.
}

现在,请注意,在构造函数中,您正在分配一个动态数组Stacks,大小为size

这里stackArray = new Stack[size]


你有两个问题

  1. 分配使用堆栈的默认构造函数,而您没有,因为您声明了自定义构造函数。
  2. 如果您使用自定义构造函数,您将获得无限递归。

您必须提供要分配的数组元素的正确类型(从其余代码来看,它似乎是 double)而不是 Stack 类型

stackArray = new double[size].

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 2012-06-11
    • 2012-10-29
    • 1970-01-01
    • 2016-03-28
    • 2019-02-09
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多