【发布时间】: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 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 ©) //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