Template list
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Template list

  1. #1
    Join Date
    May 2013
    Posts
    5

    Template list

    Write your question here.
    Hello, I'm doing a homework aasignment on templates, and i have to build a list.
    The problem starts when i am trying to add elements to the list. For instance if i chose to add 5 different elements (1,2,3,4,5) the output will be (5,5,5,5,5).
    I have no idea how to fix it. Thank you for all the help!

    Code:
    	 void add_back(T t){
    		Node* tmp = new Node;
    		tmp -> m_data = &t;
    		if(m_head ==  NULL)
    		{
    
    			m_head = tmp;
    			m_tail = m_head;
    			m_size += 1;
    		}
    		else
    		{
    			m_tail -> m_next = tmp;
    			tmp -> m_prev = m_tail;
    			m_tail = tmp;
    			m_size += 1;
    		}
    }
    Code:
    	 void add_front(T t){
    		Node* tmp = new Node;
    		tmp -> m_data = &t;
    		if(m_head ==  NULL)
    		{
    			m_head = tmp;
    			m_size += 1;
    		}
    		else
    		{
    			m_head -> m_prev = tmp;
    			tmp -> m_next = m_head;
    			m_head = tmp;
    			m_size += 1;
    		}
    	 }
    Code:
       int main()
    {	
             List<int> list;
    	if(!list.isEmpty()) 
    		cout<<"Error"<<endl;
    	list.add_back(5);
    	list.add_back(21);
    	list.add_back(34);
    	list.add_front(70);
    	cout<<list.size()<<endl; //size is 1
    	if(list.isEmpty()) 
    		cout<<"Error"<<endl;
    	cout<<list.pop_back()<<endl;
    	if(!list.isEmpty()) 
    		cout<<"Error"<<endl; 
    }
    Attached Images Attached Images  

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,308

    Re: Template list

    I have no idea how to fix it. Thank you for all the help!
    To fix it, you need to do what all of us have to do all the time - debug the code!

    Have you designed the program first before coding it? Have you checked that your code matches your program design? Have you debugged the code using the debugger to see where the program behaviour deviates from that which is expected? Have you traced through your code by hand drawing diagrams of the list structure and links as you go through it?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,308

    Re: Template list

    What's the definition of Node?

    Code:
    tmp -> m_data = &t;
    What is the definiton of m_data? What do you expect to happen with this line of code?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    May 2013
    Posts
    5

    Re: Template list

    class List{
    private:
    int m_size;
    class Node{
    public:
    T* m_data;
    Node* m_next;
    Node* m_prev;

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,308

    Re: Template list

    Code:
    T* m_data;
    So m_data is a pointer to type T.

    Code:
    void add_back(T t){
    		Node* tmp = new Node;
    		tmp -> m_data = &t;
    You are setting m_data to be the address of t - but t is local to the function add_back as it is copied by value and therefore its address is only valid during the scope of function add_back!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center