Charlie
Charlie

Reputation: 1

Undeclared identifier error when item is declared

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"

Upvotes: 0

Views: 854

Answers (1)

Klitos Kyriacou
Klitos Kyriacou

Reputation: 11664

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

Upvotes: 2

Related Questions