Linked List Testing Problems

I am continuously getting an error while attempting to test a linked list with nodes that says "the default constructor of "OListType<int> cannot be referenced -- it is a deleted function. Any help with this is greatly appreciated.

Last edited on
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
#ifndef H_OListType
#define H_OListType
#include <iostream>
#include "ListType.h"
template <class T> 
class OListType :public ListType<T> {
public:
	void insert(const T&);
};
template <class T> 
void OListType<T>::insert(const T& info) {
	NodeType<T> *curr, *prev, *temp;
	temp = new NodeType<T>;
	temp->info = info;
	curr = this->head;
	prev = nullptr;
	while ((curr != nullptr) && (info > temp->info)) {
		prev = curr;
		curr = curr->link;
	}
	if (curr == nullptr && prev == nullptr) {
		this->head = temp;
		this->head->link = curr;
	}
	else {
		prev->link = temp;
		temp->link = curr;
	}
	++this->count;
}
#endif 
1
2
3
4
5
6
7
8
9
#ifndef H_NodeType
#define H_NodeType
#include <iostream> 
template <class T> 
struct NodeType {
	T info; 
	NodeType* link; 
};
#endif 
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
#ifndef H_ListType
#define H_ListType
#include <iostream>
#include "NodeType.h"
template <class T> 
class ListType {
	ListType();
	ListType(const ListType<T>&);
	virtual ~ListType();
	const ListType<T>& operator =(const ListType<T>&);
	virtual void insert(const T&) = 0;
	virtual bool erase(const T&); 
	virtual void eraseAll();
	virtual bool find(const T&) const;
	size_t size() const;
	bool empty() const; 
	template <class U>
	friend std::ostream& operator << (std::ostream&, const ListType<U>&);
protected:
	NodeType<T>* head;
	size_t count; 
private:
	void destroy();
	void copy(NodeType<T>*&, NodeType<T>*);
};
template <class T>
ListType<T>::ListType() {
	count = 0; 
	head = nullptr; 
}
template <class T> 
ListType<T>::ListType(const ListType<T>& source) {
	copy(head, source.head);
	count = source.count;
}
template <class T> 
ListType<T>::~ListType() {
	destroy();
}
template <class T>
const ListType<T>& ListType<T>::operator=(const ListType<T>& source) {
	if (this != source) {
		destroy();
		copy(head, source.head);
		count = source.count;
	}
	return *this;
}
template <class T>
void ListType<T>::eraseAll() {
	destroy();
}
template <class T> 
size_t ListType<T>::size() const {
	return count; 
}
template <class T> 
bool ListType<T>::empty() const {
	return count == 0;
}
template <class T>
bool ListType<T>::erase(const T& info) {
	NodeType<T>* curr, * prev;
	bool found = false;
	curr = this->head;
	prev = nullptr;
	while ((curr != nullptr) && (info <= curr->info) && (!found)) {
		if (curr->info == info)
			found = true;
		else {
			prev = curr;
			curr = curr->link;
		}
	}
	if (found) {
		if (prev != nullptr) {
			prev->link = curr->link;
			delete curr;
		}
		--this->count;
	}
	return found;
}
template <class T> 
bool ListType<T>::find(const T& info) const {
	NodeType<T>* temp = this->head;
	bool found = false;
	while ((temp != nullptr) && (!found)) {
		if (temp->info == info)
			found = true;
		else
			temp = temp->link;
	}
	return found;
}
template <class T> 
void ListType<T>::destroy() {
	NodeType<T>* temp;
	while (head != nullptr) {
		temp = head;
		head = head->link;
		delete temp;
	}
	count = 0;
}
template <class T> 
void ListType<T>::copy(NodeType<T>*& node, NodeType<T>* source) {
	if (source != nullptr) {
		node = new NodeType<T>;
		node->info = source->info;
		copy(node->link, source->link);
	}
	else {
		node = nullptr;
	}
}
template <class U>
std::ostream& operator<<(std::ostream& out, const ListType<U>& source) {
	if (!source.empty()) {
		NodeType<U>* temp;
		NodeType<U>* temp = source.head;
		out << temp->info;
		temp = temp->link;
		while (temp != nullptr) {
			out << ", " << temp->info;
			temp = temp->link;
		}
	}
	return out;
}
#endif 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream> 
#include "ListType.h"
#include "NodeType.h"

template <class T> 
class UListType :public ListType<T> {
public:
	void insert(const T&);
};
template <class T> 
void UListType<T>::insert(const T& info) {
	NodeType<T> *temp;
	temp = new NodeType<T>;
	temp->info = info;
	temp->link = this->head;
	this->head = temp;
	++this->count;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "OListType.h"
#include "UListType.h"
#include "ListType.h"
#include <iostream>

using namespace std; 

int main()
{
	OListType<int> oList, oList1; //this is where i keep getting an error
	UListType<int> uList; 


}
1
2
3
class ListType {
public:                     // <===== NEEDED
	ListType();



Also:
1
2
//   	NodeType<U>* temp;        // <===== DELETE THIS LINE
	NodeType<U>* temp = source.head;
On the testing part, I am now getting an error on line 14 stating that expression must have bool type or be convertible to bool and an error on line 14 stating that conditional expression of type void is illegal??

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
#include "OListType.h"
#include "UListType.h"
#include "ListType.h"
#include <iostream>

using namespace std; 

int main()
{
	OListType<int> olist, olist1;
	UListType<int> ulist; 

	for (int i = 1; i <= 11; i = i + 10) {
		if (ulist.insert(i)) {
			cout << "insert of " << i << "succeeded" << endl; 
		}
		else {
			cout << "insert of " << i << " failed" << endl; 
		}
	}
	ulist.insert(100); 
	cout << ulist << endl; 
	for (int i = 1; i <= 11; i = i + 10) {
		if (olist.insert(i)) {
			cout << "insert of " << i << " succeeded" << endl; 
		}
		else {
			cout << "insert of " << i << " failed" << endl; 
		}
	}
	cout << olist << endl; 
	olist.insert(115);
	cout << olist << endl;

	olist1 = olist; 
	cout << "Test " << olist1 << "Test" << endl; 
	for (size_t items = 1; items <= 11; ++items) {
		if (olist.find(items)) {
			cout << items << " is in the ordered list." << endl; 
		}
	}
	olist.erase(5);
	for (size_t items = 1; items <= 11; ++items) {
		if (olist.find(items)) {
			cout << items << " is in the ordered list." << endl; 
		}
		else {
			cout << items << " is not in the ordered list." << endl; 
		}
	}
	return 0; 



}
Look at what ulist.insert() returns (and, indeed, what all your insert() functions return).

Look at what you're trying to do with what the function returns.

Hopefully, it should be obvious what's wrong.
Last edited on
line 14... what exactly is the *type* returned from the insert routine?
that result is being treated as the boolean for the if statement. If its void, that will not do. (I didnt look it up in your code, but that is what the error is saying).
Last edited on
Oh wow. Thanks guys, it was the simplest fix. I feel so dumb.
No problem - glad it helped! It's an easy mistake to make :)
Topic archived. No new replies allowed.