Archived

This topic is now archived and is closed to further replies.

Problems with a linked list template

This topic is 5585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I've been working on my game recently and I've run into a few problems with my linked list implementation. When I try to compile it gives me this error: "("line" 64) : error C2512: 'Node' : no appropriate default constructor available" I can't seem to fix that problem and I don't see why it complains about it. Here's the linked list code:
    
template<typename T>
class List
{
private:
	// A structure to hold each node

	struct Node
	{
		Node *next;  // Pointer to the next Node

		T	  data;	 // The actual data in the node

	};

	Node *m_head;	 // pointer to the first node of the list

    int  m_count;    // number of items in the list


public:
	List();	 // Constructor; Creates the linked list

	~List(); // Destructor; Destroys the linked list


	void Add(T value);						   // Adds an item to the list

	void Delete(int index);					   // Removes an item from the list

	int  Count(void) const { return m_count; } // Returns the number of items in the list

};

////////////////////////////////////////////////////////////

// List::~List

////////////////////////////////////////////////////////////

template<typename T>
List<T>::List()
{
	m_head = NULL;
	m_count = 0;
}

////////////////////////////////////////////////////////////

// List::~List

////////////////////////////////////////////////////////////

template<typename T>
List<T>::~List()
{
	if (m_count) // If there are any nodes

	{
		for (int i = 0; i < m_count; i++)
		{
			Delete();
		}
	}
}

////////////////////////////////////////////////////////////

// List::Add

////////////////////////////////////////////////////////////

template<typename T>
void List<T>::Add(T value)
{
	Node * newNode = new Node; // <-- Line 64

	newNode->data = value;
	newNode->next = m_head;
	m_head = newNode;
	m_count++;
}

////////////////////////////////////////////////////////////

// List::Delete

////////////////////////////////////////////////////////////

template<typename T>
void List<T>::Delete(int index)
{
	Node * delNode = m_head;
	Node * prevNode = NULL;
	for (int i = 0; i < index; i++)
	{
		prevNode = delNode;
		delNode = delNode->next;
	}

	if (delNode == m_head)
	{
		delNode->next = m_head;
	}
	else
	{
		prevNode->next = delNode->next;
	}

	delete delNode;
	m_count--;
}
    
Any help would be appreciated since I really want to test out my resource loading routines and I can't without this linked list working. I'm learning, just like the best of us... [edited by - Soulkeeper on August 31, 2002 5:26:03 PM]

Share this post


Link to post
Share on other sites
What template parameter are you using to test the list? It would work fine if T has a default constructor, but if not, List<T>::Node can't be constructed at line 64.

[edited by - Krunk on August 31, 2002 5:36:24 PM]

Share this post


Link to post
Share on other sites
I''m using a class I made for the maps: CMap

It doesn''t have a default constructor, it has to be constructed with a specified xsize and ysize.

So, what you''re saying is that I should create a default ctor for CMap then... hm... ok, I''ll get to work on that then.

Share this post


Link to post
Share on other sites
Not necessarily. It might be better to avoid using the default constructor of T if you can. You could try using a Node(const T &t) constructor that copies t into data. Then instead of using new T on line 64, you can use new T(value).

Doing it that way reduces the requirements on T, meaning you don''t have to have a default construtor for every type you want to use with List.

Share this post


Link to post
Share on other sites