Thread: Array in mystack

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    41

    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();
    
    
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is this:
    Code:
    int *Arr=new Arr[5]
    supposed to be
    Code:
    int *Arr=new int[5]
    ?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thanks but have error
    'MyStack::Arr' : only static const integral data members can be initialized

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thanks. i understood.
    Code:
    MyStack::MyStack(){
      size =100;
      Arr= new int[size];
      index=0;
    }
    Last edited by llynx; 10-09-2009 at 11:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM