Jump to content
  • Advertisement
Sign in to follow this  
Trillian

Linked list with templates problem

This topic is 4705 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

Hello! This is my first time making a linked list and using templates. However it works quite well except I get a bug when calling a function. The Linked list is supposed to have the following functionnality: -Supports any var types (templates) -Can always add new nodes -Each node has an index starting from 0 -Data can be accessed with the index Here's my class code:
[source lang=cpp]
template<class T>
class CD3DYXarray
{
protected:
	T * m_pData;
	CD3DYXarray * m_pNextNode;
	int m_nArrayPos;

public:
	CD3DYXarray()
	{
		m_nArrayPos=0;
		m_pData=NULL;
		m_pNextNode=NULL;
	}

	CD3DYXarray(int num)
	{
		m_nArrayPos=num;
		m_pData=NULL;
		m_pNextNode=NULL;
	}

	~CD3DYXarray()
	{
		m_nArrayPos=0;
		SAFE_DELETE(m_pData);
		SAFE_DELETE(m_pNextNode);
	}


	void SetData(T * tData) { if(tData) m_pData = tData; }

	void AddNode(T * tData)
	{
		if(m_pNextNode)
			m_pNextNode->AddNode(tData);
		else
		{
			m_pNextNode = new CD3DYXarray(m_nArrayPos+1);
			m_pNextNode->SetData(tData);
		}
	}

	T * at(int num)
	{
		if(m_nArrayPos == num) return m_pData;
		if(m_pNextNode == NULL) return NULL;
		return m_pNextNode->at(num-1);
	}

	void OutputContent()
	{
		printf("Array[%i] = %i\n",m_nArrayPos,*m_pData);
		if(m_pNextNode)
			m_pNextNode->OutputContent();
	}
};

All functions work well with an unlimited number of nodes (i've tryed 3). EXCEPT the at() function. the at() function wont work, if the argument is 0, it outputs "0" if the argument is anything else, it bugs, runtime error. I am testing it with "int*" var types. please help!

Share this post


Link to post
Share on other sites
Advertisement
I would highly recommend that rather than try to fix that [it is poorly written in several different ways], you just use std::list. Or, since you clearly want random access, std::vector.

However, the problem you are encountering is almost certainly that you're decreasing num when you recursively call at().

CM

Share this post


Link to post
Share on other sites
I haven't looked too closely so maybe I missed something. But shouldn't:


return m_pNextNode->at(num-1);


Be simply

return m_pNextNode->at(num);


since you are still looking for the same index. As for the other. I'm not sure why it's behaving the way you described. As for the returning "0". 0(Assuming you are talking about the number not a string) is NULL so it may have something to do with your pointers getting mixed up or your indexes(not being able to find the node).

Share this post


Link to post
Share on other sites
From my understanding of your at function, you have an int passed in and you're looking for the data at that number. First you check if the position you're at is that number, if it is its found so return the data. If not check if the next node is null, if it's null you didn't find that index so return null. and lastly if there is something pass num-1 into the next node?

If this is how it works, why do you pass num-1? If you're searching for a specific index, why modify it? But if I have it completely wrong please explain what you're trying to do with the at function.

Hope this helps!
darkweb

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!