Need help with finding error in class!

Hello everyone. I want to create my own List class. Problem is code compiles without errors, but in testing, reference to a specific element causes memory access exception, "Instruction at 0x4fe2b...." referenced 0x0000000(0x0000004 or 8 at the end. Memory cannot be "read". Looks like wrong reference(syntacsis) or going out of boundries, maybe something related to standard. Spent several hours but did not find the mistake.
Compiler MinGW32
List.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef LIST_H
#define LIST_H

#include <iostream>

template <class T>
class List
{
	private:
	
	struct Node
	{
		Node *prev;
		T elem;
		Node *next;
		Node(Node *aprev, const T& aelem, Node *anext):
		prev(aprev),elem(aelem), next(anext)
		{
		}
	};
	/*class Iter
	{
		friend class List;
		
		Iter():p(0)
		{
		}
		Iter(Node *ap):p(ap)
		{
		}
		const Iter& operator++()
		{
			p=p->next;
		}
		const Iter& operator--()
		{
			p=p->prev;
		}
		bool operator==(const Iter& other)
		{
			return p==other.p;
		}
		bool operator!=(const Iter& other)
		{
			return p!=other.p;
		}
		Iter begin()
		{
			return Iter(first);
		}
		Iter end()
		{
			return Iter(last);
		}
		Iter insert()
		{
			Node *t=new Node();
		}
	};*/
	public:
	int length;
	void push_back(const T& Ilem);
	void push_front(const T& Ilem);
	void pop_back();
	void pop_front();
	void insert(int position, const T& Ilem);
	void reverse(int begpos, int endpos);
	int size();
	T data(int begn);
	Node *first;
	Node *last;
	Node *p;
	List();
	~List();
};
template <class T>
List<T>::List()
{
	first = new Node(0, T(), 0);
	last = first->next;
	length=0;
}

template <class T>
void List<T>::push_back(const T& Ilem)
{
	last=new Node(last, Ilem, 0);
	last=last->next;
	length++;
}
template <class T>
void List<T>::push_front(const T& Ilem)
{
	first=new Node(0, Ilem, first);
	first=first->prev;
	length++;
}
template <class T>
void List<T>::pop_back()
{
	if(length==0)
	{
		std::cout<<"List is empty! Unable to delete from back.";
	}
	else
	{
		Node *tmp = last;
		last=last->prev;
		delete tmp;
		length--;
	}
}
template <class T>
void List<T>::pop_front()
{
	if(length==0)
	{
		std::cout<<"List is empty! Unable to delete from front.";
	}
	else
	{
		Node *tmp = first;
		first=first->next;
		delete tmp;
		length--;
	}
}
template <class T>
List<T>::~List()
{
	Node *p = first;
	while(p!=0)
	{
		Node *t=p;
		p=p->next;
		delete t;
	}
}
template <class T>
int List<T>::size()
{
	return (*this).length;
}
template <class T>
T List<T>::data(int begn)
{
	if(begn==1)
	{
		return (*this).first->elem;
	}
	else
	{
		return (*this).last->elem;
	}
}

#endif 

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "list.h"
#include <iostream>

using namespace std;

int main()
{
	List<int>a;
	int some, length;
	while(cin>>some)
	{
		a.push_back(some);
		a.push_front(some);
	}
	while(a.size()>0) // size increment works
	{
		cout<<a.data(1)<<a.data(0)<<" "; // also tried a.first->elem
		a.pop_back();
		a.pop_front();
	}
	
	return 0;
}

For any usefull info or help thanks in advance.
Last edited on
1
2
3
4
Node(Node *aprev, const T& aelem, Node *anext):
prev(aprev),elem(aelem), next(anext)
{
}


In your Node constructor, you never reset the aprev->next or anext->prev.

There may be other errors, too, but that just jumped out at me.

Just to ask a question. When you create an empty list, you put an empty Node on the list, and point first at it, and set the length to 0. I don't think that's what you want to do, is it?

If you took that list and did push_front(A), push_back(B), you will have a list with the following contents (after you fix that problem with the Node constructor):
A->empty->B
length = 2


I don't think that's what you are looking for. You probably want to set first = last = NULL in the List constructor. Then in the push_front and push_back functions, check for an empty list before inserting. The pop_front() and pop_back() functions should delete the final node and make sure first and last are set to NULL appropriately.
Last edited on
Topic archived. No new replies allowed.