What error in this code?Help me!

This is a discussion on What error in this code?Help me! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> template <class T> class Queue { private: // Local Element Class template <class T> struct Element { ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    8

    What error in this code?Help me!

    Code:
    #include <iostream.h>
    
    template <class T>
    class Queue
    {
    private:
    	// Local Element Class
    	template <class T>
    	struct Element
    	{
    		T m_info;
    		Element<T> *m_next;
    		//Default Constructor
    		Element<T>(T elemInfo, Queue<T>::Element<T> * next=NULL)
    		{
    			m_info=elemInfo;
    			m_next=next;
    		}
    	};
    
    	// Header of Linked List
    	Element<T> *m_head;
    
    public:
    	// Default Constructor
    	Queue(){ m_head=NULL; }
    	// Destructor                              
    	~Queue();
    	
    	// Insertion functions
    	void Enqueue(T&);
    	T&   Dequeue();
    	T&   Top();
    	bool isEmpty() { return m_head==NULL;}
    };
    
    
    //Destructor
    template <class T>
    Queue<T>::~Queue()
    {
    	Queue<T>::Element<T> *temp;
    	// While there is still an Element at the head of the Queue
    	while (m_head!=NULL)
    	{
    		// Keep pointer to Element
    		temp=m_head;
    		// Move to next Element
    		m_head=m_head->m_next;
    		// Free Element's memory
    		delete temp;
    	}
    }
    
    // Insert new Element to the Queue
    template <class T>
    void Queue<T>::Enqueue(T &toAdd)
    {
    	// Create new Element, with m_head as it's next
    	Queue<T>::Element<T>* temp=new Queue<T>::Element<T>(toAdd, m_head);
    	// Set Queue head to new Element
    	m_head=temp;
    }
    
    // Remove Element from Queue
    template <class T>
    T& Queue<T>::Dequeue()
    {
    	Queue<T>::Element<T>* temp;
    	Queue<T>::Element<T>* temp2;
    	T* returnValue;
    	temp=temp2=m_head;
    	
    	// If Stack is not empty
    	if (temp!=NULL)
    	{
    		// While temp is not last Element
    		temp=temp->m_next;
    		while (temp!=NULL && temp->m_next!=NULL)
    		{
    			// Move to next Element
    			temp=temp->m_next;
    			temp2=temp2->m_next;
    		}
    		temp2->m_next=NULL;
    		// Return Element
    		if (temp!=NULL)
    		{
    			returnValue=new T(temp->m_info);
    			delete temp;
    			return *returnValue;
    		}
    		else
    		{
    			returnValue=new T(m_head->m_info);
    			delete m_head;
    			m_head=NULL;
    			return *returnValue;
    		}
    	}
    	else
    		throw "Queue is empty!";
    }
    
    // Get first Element in Queue
    template <class T>
    T& Queue<T>::Top()
    {
    	Queue<T>::Element<T>* temp;
    	Queue<T>::Element<T>* temp2;
    	temp=temp2=m_head
    	
    	// If Stack is not empty
    	if (temp!=NULL)
    	{
    		// While temp is not last Element
    		temp=temp->m_next;
    		while (temp!=NULL)
    		{
    			// Move to next Element
    			temp=temp->m_next;
    			temp2=temp2->m_next;
    		}
    		// Return Element
    		return temp->m_info;
    	}
    	else
    		throw "Queue is empty!";
    }
    
    
    
    
    
    class point
    {
    public:
    	int x,y;
    	// Default constractor
    	point (int n1=0, int n2=0):x(n1),y(n2){};
    	// Default copy constractor
    	point (point &p2):x(p2.x),y(p2.y){};
    
    	// Assignment operator
    	point& operator=(point &p2){ x=p2.x;y=p2.y; return *this;};
    	
    	// operator !=
    	bool operator!=(point &p2)
    	{
    		if (p2.x==x && p2.y==y)
    			return false;
    		return true;
    	}
    	// operator ==
    	bool operator==(point &p2)
    	{
    		if (p2.x==x && p2.y==y)
    			return true;
    		return false;
    	}
    	// Basic display function
    	void print() { cout<<"( "<<x<<" , "<<y<<" )"<<endl; }
    };
    
    
    
    void main()
    {
    	Queue<point> QU;
    	point a(1,2), b(3,4), c(5,6), d(10,10), e(0,0), f(8,4);
    	point t;
    	
    	cout<<"Adding items to the Queue..."<<endl;
    	QU.Enqueue(a);
    	QU.Enqueue(b);
    	QU.Enqueue(c);
    	QU.Enqueue(d);
    	QU.Enqueue(e);
    	QU.Enqueue(f);
    
    	cout<<"Printing Queue..."<<endl;
    	while (!QU.isEmpty())
    	{
    		t=QU.Dequeue();
    		t.print();
    	}
    	
    }
    I can't run it.What error?How to fix it,profs?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    > I can't run it.What error?

    What error? That is what we ask you... what error is it showing?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What error?
    void main. That's instant death around here.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by Prelude
    >What error?
    void main. That's instant death around here.
    Salom doesn't like void main() very much :P

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Amazing. I never catch that one.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,453
    1. after deleting the element the pointer pointing to it is not set to NULL (so you get dangling pointer in your queue
    2. After returning element in the DeQueue function you get the memory leak, because there is no delete corresponding to returnValue=new T(temp->m_info);

    Maybe there are other
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Salom doesn't like void main() very much :P
    None of us do. Salem simply has enough spare time to create an avatar for it.
    My best code is written with the delete key.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    This:
    Code:
    #include <iostream.h>
    Gaaaah! This won't even work on most modern compilers. It should be:
    Code:
    #include <iostream>
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    None of us do. Salem simply has enough spare time to create an avatar for it.
    I asked salem about that, he gave quite an intersting answer too. Check out the general discussions board under "salems buddy". It took him a while to make it, I think its very clever.
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21