class template
when debug,occur SIGSEGV default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <string>
using namespace std;
template<typename T>
class Stack
{
public:
Stack(int size);
~Stack();
bool isEmpty();
bool isFull();
void push(T data);
T pop();
private:
T *space;
int top;
int maxsize;
};
template<typename T>
Stack<T>::Stack(int size)
{
space=new T[size];
top=-1;
maxsize=size;
}
template<typename T>
Stack<T>::~Stack()
{
delete[] space;
}
template<typename T>
bool Stack<T>::isEmpty()
{
return top==-1;
}
template<typename T>
bool Stack<T>::isFull()
{
return top==maxsize-1;
}
template<typename T>
void Stack<T>::push(T data)
{
space[top++]=data;
}
template<typename T>
T Stack<T>::pop()
{
return space[--top];
}
int main()
{
Stack<string> s(100);
for(int i=0;i<10;i++)
{
if(!s.isFull())
s.push("x");
}
while(!s.isEmpty())
{
cout<<s.pop()<<" ";
}
return 0;
}
|
On line 63 the line 27 is called.
On first time of line 66, the line 52 is called.
Therefore, you effectively call:
space[ -1 ] = data;
thanks . should be ++top in push() and top-- in pop()
Topic archived. No new replies allowed.