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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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
  •  





Click Here to Expand Forum to Full Width

Featured