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.
You must understand that in your code, t is basically a local variable (a value copy of the original parameter that was passed).
As soon as the add_front() / add_back() function ends, t "dies" and the memory address of t which you're storing in tmp->m_data becomes invalid.
Also, even though you haven't posted the complete code, I feel the need to remind you that C++ is not a garbage-collected language; this means that every new must have its corresponding delete otherwise a memory leak may occur. You did delete your new'd memory, didn't you?
Finally your algorithm is a bit strange, are you sure you're supposed to be working on a doubly-linked list, and also keep track of the "tail"?
void add_front(T& t)
This passes t by reference, and is probably not what you want. The & when put between a variable name and its type means "reference" just as an * between a variable and its type means "pointer". It is not the & (address-of) operator that you've been using so far. So don't do this.