const problems :(

hi,
i'm trying to implement a linkedlist with iterators in c++,
but when i compile i get :
1)

./list.h: In instantiation of ‘T& List<T>::Iterator::operator*() [with T = int]’:
./listExample.cpp:25:3: required from here
./list.h:183:35: error: invalid conversion from ‘const List<int>::ListNode*’ to ‘List<int>::ListNode*’

2)

./list.h: In instantiation of ‘List<T>::List(const List<T>&) [with T = int]’:
./listExample.cpp:56:28: required from here
./list.h:244:22: error: base operand of ‘->’ has non-pointer type ‘const List<int>’



here's the relevant code :
[u]the API:[/u]
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
template<class T>
class List {
private:
	//=========================================================================
	class ListNode {
	public:
		ListNode(ListNode& node);
		ListNode(T& element);
		ListNode& operator=(const ListNode& node) const;
		T& getData();
		const T& getData() const;
		~ListNode();
	private:
		ListNode* next;
		ListNode* previous;
		T* data;
		friend class List<T>::Iterator;
		friend class List<T>;
	};
	//=========================================================================
public:
	List();
	List(const List<T>& list);
	~List();
	List& operator=(const List<T>& list) const;
	bool operator==(const List<T>& list) const;
	bool operator!=(const List<T>& list) const;
	//=========================================================================
	class Iterator {
	public:
		Iterator(const Iterator& it);
		Iterator& operator++();
		Iterator operator++(int);
	        const T& operator*() const;
	        T& operator*();
		bool operator==(const Iterator& it) const;
		bool operator!=(const Iterator& it) const;
	private:
		int index;
		const List<T>* list;
		Iterator& operator--();
		Iterator(const List<T>* list,int index);
		friend class List<T>;
	};
	//=========================================================================
	class ConstIterator {
	public:
		ConstIterator(const ConstIterator& it);
		ConstIterator(List<T>::Iterator& it);
		const ConstIterator& operator++();
		const ConstIterator operator++(int);
		const T& operator*() const;
		bool operator==(const ConstIterator& it) const;
		bool operator!=(const ConstIterator& it) const;
	private:
		int index;
		const List<T>* list;
		ConstIterator(const List<T>* list,int index);
		friend class List<T>;
	};
	//=========================================================================
	Iterator begin();
	Iterator end();
	ConstIterator begin() const;
	ConstIterator end() const;
	void insert(T element);
	void insert(T element,Iterator position);
	void remove(Iterator position);
	int getSize() const;

	template<class FindFunction>
	Iterator find(FindFunction f);
	template<class FindFunction>
	ConstIterator find(FindFunction f)const;

	template<class SortFunction>
	void sort(SortFunction f);


private:
	ListNode* getNode(Iterator it);
	const ListNode* getNode(Iterator it) const;
	const ListNode* getNode(ConstIterator it) const;
	void swapData(Iterator it1,Iterator it2);
	void removeHead();
	void removeTail();
	void removeMiddle(Iterator position);
	ListNode* head;
	int size;
	string type;
};


[u]the Implementation:
for problem 1 :
[/u]
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
template<class T>
const T& List<T>::Iterator::operator*() const{
	assert(index >= 0 && index < list->getSize());
	ListNode *node = list->getNode(*this);
	return node->getData();
}
template<class T>
T& List<T>::Iterator::operator*(){
	assert(index >= 0 && index < list->getSize());
	Iterator it(*this);
	ListNode *node = list->getNode(it);
	return node->getData();
}

template<class T>
T& List<T>::ListNode::getData(){
	return *data;
}
template<class T>
const T& List<T>::ListNode::getData() const{
	return *data;
}

template<class T>
typename List<T>::ListNode* List<T>::getNode(List<T>::Iterator it){
	int i=0;
	ListNode* ptr = head;	
	while(it.index != i){
		ptr = ptr->next;
		i++;
	}
	return ptr;
}
template<class T>
const typename List<T>::ListNode* List<T>::getNode(List<T>::Iterator it) const{
	int i=0;
	const ListNode* ptr = head;	
	while(it.index != i){
		ptr = ptr->next;
		i++;
	}
	return ptr;
}


[u]the Implementation:
for problem 2 :
[/u]
1
2
3
4
5
6
7
8
9
10
template<class T>
List<T>::List(const List<T>& list):head(new ListNode(*(list->head))),size(list.getSize()),
type(typeid(T).name()){}

template<class T>
List<T>::ListNode::ListNode(ListNode& node){
	data = new T(*(node.data));
	next = new ListNode(*next);
	previous = new ListNode(*previous);
}



does anyone have a solution ? please tell me .
if anyone have a suggestion to make the list better or thinks my implementation not quit right please help your self .
Last edited on
i fixed it thanks
changed
ListNode(ListNode& node);
to
ListNode(const ListNode& node);
changed
const List<T>* list;
to
List<T>* list;
changed
head(new ListNode(*(list->head)))
to
head(new ListNode(*(list.head)))
Topic archived. No new replies allowed.