Thread: I wrote a piece of code, can't find where is the bug....

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    I wrote a piece of code, can't find where is the bug....

    Dear All:

    Here is the code I wrote.

    Code:
    #include <iostream>
    using namespace std;
    
    template <typename T>
    class Stack;
    
    template <typename T>
    class Node{
    private:
    	Node * next;
    	T* data;
    public:
    	friend class Stack<T>;
    	Node():next(0),data(0){}
    	Node( Node * head,  T * value):next(head),data(value){}
    	Node * GetNext(){ return next; }
    	void SetNext(const Node* newnext) {next = newnext;}
    	T* GetData() { return data; }
    	void SetData(const T* newdata) { data = newdata;}
    	virtual ~Node(){}
    };
    
    template <typename T>
    class Stack{
    public:
    	Stack():head(0){}
    	void Push( T* data);
    	T * Pop();
    	T * Top();
    	virtual ~Stack();
    private:
    	Node<T> * head;
    };
    
    template <typename T>
    void Stack<T>::Push( T* data){
    	head = new Node<T>(head,data);
    }
    
    template <typename T>
    T* Stack<T>::Pop(){
    	if (!head) return NULL;
    	Node<T> * oldhead = head;
    	T * result = head->GetData();
    	head = head->GetNext();
    	delete(oldhead);
    	return result;
    }
    
    template <typename T>
    T* Stack<T>::Top(){
    	if (!head) return NULL;
    	return (head->GetData());
    }
    
    template <typename T>
    Stack<T>::~Stack(){
    	while(head){ 
    		Node<T>* oldhead = head;
    		head = head->GetNext();
    		delete(oldhead);
    	}
    }
    
    
    
    int main(){
    	Stack<int>* test = new Stack<int>;
    	int x=1,y=2,z=3;
    	test->Push(&x);
    	test->Push(&y);
    	test->Push(&z);
    	cout<<*(test->Top())<<endl;
    	test->Pop();
    	cout<<*(test->Top())<<endl;
    	delete(test);
    
    	return 0;
    }
    This code implements a Stack.
    I tried to declare: (with const)
    Code:
    Node( const Node * head,  const T * value):next(head),data(value){}
    But it fails to complie... I am so confused....

    BTW, I would polish this Stack as safe as possible, any suggestions to improve the robustness?
    Last edited by meili100; 06-07-2007 at 05:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't find the BUG
    By keizer in forum C Programming
    Replies: 4
    Last Post: 08-10-2008, 05:03 AM
  2. Can't Find Bug in basic MP3 Sorter
    By trickeastcs in forum C++ Programming
    Replies: 12
    Last Post: 12-14-2007, 05:31 PM
  3. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM
  4. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  5. HELP! can't find that one error in my code
    By andyt2000 in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2001, 09:25 PM