For the life of me, I can't figure out what I am doing wrong! It has been sometime since I have had use for my own list type, but since std::vector is pretty worthless for what I need it to do, I need to relearn templates.

#include <memory.h>

template <typename T>
class list
{
    struct item
    {
	item()
        {
            data = 0;

            forward = 0;
	    backward = 0;
        }

	T* data;

	item* forward;
	item* backward;
     };

	int _size;

	item* _tail;
	item* _head;

	public:

	    list();

	    void Push(T* item);

	    T& operator[](int index);
};

template <typename T>
list<T>::list()
{
    _size = 0;

    _head = 0;
    _tail = 0;
}

template <typename T>
void list<T>::Push(T* item)
{
	if(_head == 0)
	{
		item* firstItem = new item();
		memcpy(firstItem->data, item, sizeof(T));

		_head = firstItem;
		_tail = firstItem;
	}
	else
	{		
		item* temporaryTail = _tail;

		_tail = new item();
		memcpy(_tail->data, item, sizeof(T));
		
		temporaryTail->forward = _tail;
		_tail->backward = temporaryTail;

		_tail->forward = 0;

		temporaryTail = new item();
		delete temporaryTail;
	}

	//Done pushing new object, increment size.
	_size++;
}

template <typename T>
T& list<T>::operator[](int index)
{
	item* itr = _head;

	//Move to the desired index.
	for(int i = 0; i < index; i++)
	{
		itr = itr->forward;				
	}

	return *itr->data;
}

struct testData
{
	testData()
	{
		number = 0;
		character = ' ';
	}

	int number;
	char character;
};

int main()
{
	testData data1;
	data1.number = 7;
	data1.character = 'k';

	testData data2;
	data2.number = 3426;
	data2.character = 'd';

	list<testData> myList;

	myList.Push(&data1);
	myList.Push(&data2);
}

What is the problem? If you can't use std::vector, then why not use std::list? Why are you using memcpy? Just use operator= for more clarity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.