这是我的静态堆栈类的头文件,它直接从我的教科书中复制,就像它应该是
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
// Stack template
template <class T>
class Stack
{
private:
T *stackArray;
int stackSize;
int top;
public:
// Constructor
Stack(int);
// Copy constructor
Stack(const Stack&);
// Destructor
~Stack();
// Stack operations
void push(T);
void pop(T &);
bool isFull();
bool isEmpty();
};
//***************************************************
// Constructor *
//***************************************************
template <class T>
Stack<T>::Stack(int size)
{
stackArray = new T[size];
stackSize = size;
top = −1;
}
//***************************************************
// Copy constructor *
//***************************************************
template <class T>
Stack<T>::Stack(const Stack &obj)
{
// Create the stack array.
if (obj.stackSize > 0)
stackArray = new T[obj.stackSize];
else
stackArray = nullptr;
// Copy the stackSize attribute.
stackSize = obj.stackSize;
// Copy the stack contents.
for (int count = 0; count < stackSize; count++)
stackArray[count] = obj.stackArray[count];
// Set the top of the stack.
top = obj.top;
}
//***************************************************
// Destructor *
//***************************************************
template <class T>
Stack<T>::~Stack()
{
if (stackSize > 0)
delete[] stackArray;
}
//*************************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************************
template <class T>
void Stack<T>::push(T item)
{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = item;
}
}
//*************************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//*************************************************************
template <class T>
void Stack<T>::pop(T &item)
{
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
item = stackArray[top];
top--;
}
}
//*************************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//*************************************************************
template <class T>
bool Stack<T>::isFull()
{
bool status;
if (top == stackSize − 1)
status = true;
else
status = false;
return status;
}
//*************************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//*************************************************************
template <class T>
bool Stack<T>::isEmpty()
{
bool status;
if (top == −1)
status = true;
else
status = false;
return status;
}
#endif
这是我的main.cpp
#include "Stack.h"
#include <stack>
#include <string>
int PostfixCalculator(string postfixExpression);
bool isOperator(const string& expression);
void performOp(const string& expression, stack<int>& calc);
int main(){
string expression;
cout << "Enter Postfix Expression" << endl;
cin >> expression;
PostfixCalculator(expression);
}
bool isOperator(const string& expression){
string ops[] = { "-", "+", "*", "/" };
for (int i = 0; i < 4; i++){
if (expression == ops[i]){
return true;
}
}
return false;
}
void performOp(const string& expression, Stack<int>& calc){//const
int leftVal, rightVal, result;
calc.pop(rightVal);
calc.pop(leftVal);
if (expression == "-"){
result = leftVal - rightVal;
}
else if (expression == "+"){
result = leftVal + rightVal;
}
else if (expression == "*"){
result = leftVal * rightVal;
}
else{
result = leftVal / rightVal;
}
cout << result << endl;
calc.push(result);
};
int PostfixCalculator(string expression){
int num;
int size = expression.size();
Stack<int> calc(size);
Stack<int> copyCalc(calc);
for (int i = 0; i < expression.size(); i++){
char c = expression.at(i);
if (c >= '0' && c <= '9'){
c = num;
copyCalc.push(num);
}
else if (isOperator(expression)){
performOp(expression, copyCalc);
}
}
};
我一直收到此错误:“错误2错误C2065:' - 1':未声明的标识符
错误发生在这里:
template <class T>
Stack<T>::Stack(int size)
{
stackArray = new T[size];
stackSize = size;
top = −1;
}
它发生在“top = -1”
答案 0 :(得分:2)
*{ margin:0; }
#popup-background
{
position:fixed;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
#popup-modal
{
padding: 20px;
width:500px;
height: 350px;
background: #EFEFEF;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
没有使用ASCII减号,而是使用了印刷标点符号。请尝试使用-1代替。