0
votes

This is my header file for my static stack class, which is straight copied from my text book like it was supposed to be

#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

this is my 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);
        }

    }

};

I keep getting this error: "Error 2 error C2065: '−1' : undeclared identifier

The error is happening here:

template <class T>
Stack<T>::Stack(int size)
{
    stackArray = new T[size];
    stackSize = size;
    top = −1;
}

it happens at "top = -1"

1
We're not psychics, tell us which line gives the error. Better yet, copy/paste the actual error from the compiler. - Mark Ransom
sorry i noticed that and did - Charlie
Your textbook puts using namespace std; in a header? That's a terrible practice. - Pete Becker
^ yes it does, why is that? - Charlie
Namespaces exist for a reason. Blowing away std:: might be acceptable in a .cpp file, but imposing it on users through a header file is bad manners. - Pete Becker

1 Answers

2
votes

The −1 is not using the ASCII minus sign, but instead it's a typographical punctuation. Try using -1 instead.