-
Array in mystack
i have write phase code to Mystack, but its error in declare dynamic array, can you help me, fix it? thanks
Code:
#include "iostream"
using namespace std;
class MyStack{
public:
MyStack();
~MyStack();
int Pop();
void Push(int item);
void IsFull();
void IsEmpty();
private:
//int this->size=5;
int *Arr=new Arr[5];
int index=-1;
};
MyStack::MyStack();
MyStack::~MyStack(){
delete(Arr);
}
int MyStack::Pop(){
index=index -1;
return Arr[index--];
}
void MyStack::Push(int item){
Arr[index]=item;
index++;
}
void MyStack::IsFull(){
if((size-1)==index)
cout<<" Stack is full";
}
void MyStack::IsEmpty(){
if(index==-1)
cout<<"Stack is Empty";
}
void main(){
MyStack s;
s.Push(6);
s.Push(3);
s.Push(2);
s.Push(5);
cout<<s.Pop();
}
-
Is this:
Code:
int *Arr=new Arr[5]
supposed to be
Code:
int *Arr=new int[5]
?
-
thanks but have error
'MyStack::Arr' : only static const integral data members can be initialized
-
Well yes that's true too; I missed that. All these things are supposed to be done in the constructor, not the definition of the class.
-
thanks. i understood.
Code:
MyStack::MyStack(){
size =100;
Arr= new int[size];
index=0;
}