Jump to content
  • Advertisement
Sign in to follow this  
SticksandStones

Templated linked list: error on passing reference.

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

Ok, I'm trying to implament a linked list for the heck of it. So, after coming up with a bit of code, I compiled it and ran it to get what I guess is Windows XP version of an illegal error: "Please send this information to Microsoft". I ran Visual C++'s debugger and found out that the error is in this code:
#include "stdafx.h"
#include <iostream>
#include "mylinkedlist.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int a = 1;
	mylinkedlist<int> mll;
	mll.push(&a);
	std::cout<<mll.getdata();
	int b;
	std::cin>>b;
	return 0;
}

on the line: mll.push(&a); Here is the function for mylinkedlist::push:
	T* push(T* value)
	{
		if(this->head == NULL)
		{
			this->currentnode = this->head;
			return this->head->data = value;
		}
		else
		{
			this->currentnode->next->data = value;
			this->currentnode = this->currentnode->next;
			return this->currentnode->data;
		}
	};

Any idea of what could be the problem? Here is the full definition of the class, if it is needed:
template <class T> struct node;
template <class T> class mylinkedlist
{
public:
	//T* assign_head(T* value){return this->head->data = value;};
	//T* addlink(T* value){return this->currentnode->next->data = value};
	T getdata(){return (*this->currentnode->data);};
	T* push(T* value)
	{
		if(this->head == NULL)
		{
			this->currentnode = this->head;
			return this->head->data = value;
		}
		else
		{
			this->currentnode->next->data = value;
			this->currentnode = this->currentnode->next;
			return this->currentnode->data;
		}
	};
	mylinkedlist(){this->currentnode = this->head;};
	~mylinkedlist(){};
private:
	bool start;
	node<T>* head;
	node<T>* currentnode;
};

template <class T> struct node
{
	T* data;
	node<T>* next;
};

Any help is appreciated. Update: I've narrowed down the problem: it is either the testing of this->head == NULL, or something is wrong in the "else" statement.

Share this post


Link to post
Share on other sites
Advertisement
You might want to:


  1. Initialize this->head.
  2. Check for NULL before any access to this->currentnode members.
  3. Check the values of the variables in the debugger when the thing crashes.

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!