why I'm getting error C2440

I'm trying to create an inner iterator to class that has an inner class.

this is my code:

main:
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
#include <iostream>
#include "DoubleEndedList.h"
using namespace std;

void main()
{
	//int element;
	DoubleEndedList<int> ls1,ls2;

	for(int i=0;i<5;i++)
	{
		ls1.add(i);
		cout << i << " ";
	}
	
	DoubleEndedList<int>::iterator it=ls1.begin();
	DoubleEndedList<int>::iterator it2=ls1.end();
	
	++it;
	++it;
	++it;
	++it;

	/*for(; it!=ls1.end(); it++)
		cout<<(*it);*/


	/*for(int i=0;i<5;i++)
	{
		ls1.add(i);
		cout << i << " ";
	}
	for(int i=0;i<5;i++)
	{
		element=ls1.firstElement();
		if(i<2)
			ls1.removeFirst();
		cout << element << " ";
		ls2.addToEnd(element);
	}
	cout << endl;
	cout << ((ls2.includes(1))? "ls2 includes 1" : "ls2 doesn't Include 1") << endl;
	cout << ((ls2.includes(2))? "ls2 includes 2" : "ls2 doesn't Include 2") << endl;
	ls2.removeFirst();
	cout << ((ls2.includes(2))? "ls2 includes 2" : "ls2 doesn't Include 2") << endl;*/


	system("pause");
}


DoubleEndedList class:
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
//--------------------------------------------
// class DoubleEndedList
// a variation on Lists - can add elements 
// to the end as well as to front
//--------------------------------------------
#ifndef DoubleEndedList_H
#define DoubleEndedList_H

#include "list.h"
#include <iostream>

using namespace std;

template <class T>
class DoubleEndedList : public List<T> 
{
public:
	// constructors
	DoubleEndedList ();

	// override the following methods from 	class List 
		void add(int value);
		void clear();
		void removeFirst();
		// add a new element to the end of the List
		void addToEnd(int value);



		//******************* iterator *******************
		class iterator; 
		friend class iterator; 

		class iterator
		{
			//DoubleEndedList & d;
			typename DoubleEndedList::Link *link;

		public:
			//iterator(DoubleEndedList<T> &_d) : d(_d) {} 

		iterator(typename DoubleEndedList<T>::Link *_d) : link(_d) {}
		iterator(typename DoubleEndedList<T>::Link *_d, bool)  : link(_d) {}


		void operator++() { // Prefix form
		  //require(index < s.top, 
			//"iterator moved out of range");
		  //return s.stack[++index];
			d.removeFirst();
		}
		//T operator++(int) { // Postfix form
		//  require(index < s.top, 
		//	"iterator moved out of range");
		//  return s.stack[index++];
		//}


		};
		
		//******************* end iterator ***************
		iterator begin() { return iterator(*this->first); }
		iterator end() { return iterator(*this->last); }






	protected:
		// data area -- Link to end
		Link  * last;
	};
#endif	
	
//------------------------------------------------
//  class DoubleEndedList implementation
//------------------------------------------------
template <class T>
DoubleEndedList<T>::DoubleEndedList() :List<T>(), last(NULL)
{}

template <class T>
void DoubleEndedList<T>::add(int val)
{  
	// add an element to the front of a double 
	// ended List only need to handle addition to
	// empty List
	if (isEmpty()) {
		List<T>::add(val);
		last=first; 
	}
	else
		List<T>::add(val);
}

template <class T>
void DoubleEndedList<T>::clear()
{   
	// delete all values from collection
	List<T>::clear();
	// then set the pointer to the last element to z
	last = NULL;
}

template <class T>
void DoubleEndedList<T>::removeFirst(){
	// remove the first element 
	List<T>::removeFirst();
	// if we remove last element
	if (isEmpty())
		last = NULL;
}

template <class T>
void DoubleEndedList<T>::addToEnd(int val)
{
	// add a new element to end of a double ended List
	if (last != NULL)
	{
		last->next = new Link(val,NULL);
		last = last->next;
	}
	// otherwise, just add to front
	else
		add(val);
}


List class:
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
158
159
160
161
//------------------------------------------------
//  class List
//      arbitrary size Lists
//      permits insertion and removal 
//      only from the front of the List
//------------------------------------------------
#ifndef List_H
#define List_H

#define NULL 0

template <class T>
class List
{
protected:
	//--------------------------------------------
	//  inner class link
	//  a single element for the linked List 
	//--------------------------------------------
	class Link
	{
	public:
		// constructor
		Link( int linkValue, Link* nextPtr);
		Link(const Link &);
		// data areas
		int value;
		Link * next;
	};	//end of class Link
public:
	// constructors
	List();
	List(const List&);
	~List();

	// operations
	void add( int value);
	int firstElement() const;
	int includes(const int &value) const;
	bool isEmpty() const;
	void removeFirst();
	void clear();

protected:
	// data field
	Link* first;
};
#endif

//------------------------------------------------
//  class Link implementation
//------------------------------------------------
template <class T>
List<T>::Link::Link(int val, Link* nxt) : value(val), next(nxt)   {}

template <class T>
List<T>::Link::Link(const Link& source) : value(source.value),next(source.next)  {}
//--------------------------------------------
//  class List implementation
//--------------------------------------------
template <class T>
List<T>::List(): first(NULL)
{
	// no further initialization
}

template <class T>
List<T>::List(const List &l) 
{
	Link *src, *trg;
	if(l.first==NULL)
		first=NULL;
	else
	{
		first= new Link((l.first)->value, NULL);
		src=l.first;
		trg=first;
		while(src->next!=NULL)
		{
			trg->next= new Link((src->next)->value, NULL);
			src=src->next;
			trg=trg->next;
		}
	}
}

template <class T>
List<T>::~List()
{
	clear();
}

template <class T>
void List<T>::clear()
{
	// empty all elements from the List
	Link* next;
	for (Link * p=first; p != NULL; p=next)
	{
		// delete the element pointed to by p
		next=p->next;
		p->next=NULL;
		delete p;
	}
	// mark that the List contains no elements
	first=NULL;
}

template <class T>
bool List<T>::isEmpty() const
{
	// test to see if the List is empty
	// List is empty if the pointer to the first
	// Link is null

	return first == NULL;
}

template <class T>
void List<T>::add( int val)
{
	//Add a new value to the front of a Linked List
	first=new Link(val, first);
	if(first==NULL) 
		throw "failed in memory allocation";
}

template <class T>
int List<T>::firstElement() const
{
	// return first value in List
	if (isEmpty())
		throw "the List is empty, no first Element";
	return first->value;
}

template <class T>
int List<T>::includes(const  int &val) const
{
	// loop to test each element
	for (Link* p=first; p!=NULL ; p=p->next)
		if (val == p->value)
			return true;
	// not found
	return false;
}

template <class T>
void List<T>::removeFirst()
{
	// make sure there is a first element
	if(isEmpty())
		throw "the List is empty, no Elements to move";
	// save pointer to the removed node
	Link* p=first;
	// reassign the first node
	first=  p->next;
	p->next = NULL;
	// recover memory used by the first element
	delete p;
} 


Last edited on
closed account (zb0S216C)
MSDN wrote:
"C2440: The compiler cannot cast from 'type1' to 'type2'."

What line does the error occur? I'm not going to read through all of that without some direction; nobody will.

Wazzak
Last edited on
line 42
Some adjustments I made to get it to compile.
I have marked the lines //<<<<
if you can't figure out the adjustments then I'll explain them.



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
//--------------------------------------------
// class DoubleEndedList
// a variation on Lists - can add elements 
// to the end as well as to front
//--------------------------------------------
#ifndef DoubleEndedList_H
#define DoubleEndedList_H

#include "list.h"
#include <iostream>

using namespace std;

template <class T>
class DoubleEndedList : public List<T> 
{
public:
	// constructors
	DoubleEndedList ();

	// override the following methods from 	class List 
		void add(int value);
		void clear();
		void removeFirst();
		// add a new element to the end of the List
		void addToEnd(int value);



		//******************* iterator *******************
		class iterator; 
		friend class iterator; 

		class iterator
		{
			//DoubleEndedList & d;
			typename DoubleEndedList::Link *link;

		public:
			//iterator(DoubleEndedList<T> &_d) : d(_d) {} 

		iterator(typename DoubleEndedList::Link *_d) : link(_d) {} //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		iterator(typename DoubleEndedList::Link *_d, bool)  : link(_d) {} //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


		void operator++() { // Prefix form
		  //require(index < s.top, 
			//"iterator moved out of range");
		  //return s.stack[++index];
			//d.removeFirst();//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<I comment this line out _ I think it is a typo
		}
		//T operator++(int) { // Postfix form
		//  require(index < s.top, 
		//	"iterator moved out of range");
		//  return s.stack[index++];
		//}


		};
		
		//******************* end iterator ***************
		iterator begin() { return iterator(this->first); } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		iterator end() { return iterator(this->last); }//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<






	protected:
		// data area -- Link to end
		typename List<T>::Link  * last;  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	};
#endif	
	
//------------------------------------------------
//  class DoubleEndedList implementation
//------------------------------------------------
template <class T>
DoubleEndedList<T>::DoubleEndedList() :List<T>(), last(NULL)
{}

template <class T>
void DoubleEndedList<T>::add(int val)
{  
	// add an element to the front of a double 
	// ended List only need to handle addition to
	// empty List
	if (List <T>::isEmpty()) {  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		List<T>::add(val);
		last= List<T>::first; 
	}
	else
		List<T>::add(val);
}

template <class T>
void DoubleEndedList<T>::clear()
{   
	// delete all values from collection
	List<T>::clear();
	// then set the pointer to the last element to z
	last = NULL;
}

template <class T>
void DoubleEndedList<T>::removeFirst(){
	// remove the first element 
	List<T>::removeFirst();
	// if we remove last element
	if (List<T>::isEmpty())  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		last = NULL;
}

template <class T>
void DoubleEndedList<T>::addToEnd(int val)
{
	// add a new element to end of a double ended List
	if (last != NULL)
	{
		last->next = new typename DoubleEndedList::Link(val,NULL); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		last = last->next;
	}
	// otherwise, just add to front
	else
		add(val);
}
Topic archived. No new replies allowed.