-1

can any one please identify the mistake in my code???
my code is::

#include<iostream>

using namespace std;

class Stack{
    int *arr;
    int size;
    int top;
    
    public:
        Stack(){arr = NULL; top = 0;}
        Stack(int sz){
                arr = new(nothrow) int[sz];
                if(!arr) exit(1);
                
                size  = sz; 
                top = 0;
        }
        ~Stack(){delete []arr;}
        int pop();
        void push(int i);
        bool isEmpty();
        bool isFull();
};

int Stack::pop()
{
        top--;
        return arr[top];
}

void Stack::push(int x)
{ 
        arr[top] = x;
        top++;
}

bool Stack::isFull()
{
    return (top<size) ? false : true;
}

bool Stack::isEmpty()
{
    return (top>0) ? false : true;
}

void pushElem(Stack &s, int i)
{
    if(!s.isFull())
    {
        cout << "Pushing " << i << " into stack" << endl;
        s.push(i);
    }
    else{
        cout << "Error: Stack is full" << endl;
    }
}

int popElem(Stack &s)
{
    if(!s.isEmpty())
    {   
        int ret = s.pop();
        cout << "Popping " << ret << " from stack" << endl;
        return ret;
    }
    else
    {
     cout << "Error: Stack is empty" << endl;
     return -1;
    }
}

int main()
{
    cout << "Stack Example" << endl;
    Stack s(10);
    pushElem(s, 1);
    pushElem(s, 2);

    popElem(s);
    popElem(s);
    
    
    for ( int i = 0; i<11; i++)
        pushElem(s, i*10);
    
    for ( int i = 0; i<11; i++)
        popElem(s);
    
}

when i compiled it the following error is exhibited:
$g++ -o try tryone.cpp
tryone.cpp: In constructor ‘Stack::Stack(int)’:
tryone.cpp:14: error: ‘exit’ was not declared in this scope

3
Contributors
3
Replies
27
Views
8 Years
Discussion Span
Last Post by Stinomus
0

Mistake 1 : Put your codes inside code tags or else no one will care to look at it.

Mistake 2 : exit() is a function and it is defined inside cstdlib header with respect to c++ and you haven't included it in the headers so the error not defined in this scope.

0

Mistake 1 : Put your codes inside code tags or else no one will care to look at it.

Mistake 2 : exit() is a function and it is defined inside cstdlib header with respect to c++ and you haven't included it in the headers so the error not defined in this scope.

Very much Thanks .I forgot to see that..

1

The cstdlib header contains the definition for the exit function as csurfer said, however up until recently this header was generally included by dependency - often unnecessarily - by other headers. So you are likely to see tutorials and books that rely on this - and other - header dependencies that have now been cleaned up.

For example you may just #include <iostream> to use the exit function in gcc 3.4 because iostream includes cstdlib however in gcc 4.3 this dependency has been removed so you need to #include <cstdlib> to utilise it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.